2013-01-16 72 views
0

數據綁定我有WFP形式那樣:WPF用戶控件使用XAML

public partial class MediaPlayerControlMain : Window 
{ 
    MediaPlayerMain MediaPlayerMain; 

    public MediaPlayerControlMain() 
    { 
     MediaPlayerMain = new MediaPlayerMain(); 
     InitializeComponent(); 
    } 
} 

我有使用MediaPlayerMain對象我的用戶控制列表(PlayList)。 用戶控制有:

public partial class PlayList : UserControl 
{ 
    public MediaPlayerMain MediaPlayer 
    { 
     get { return (MediaPlayerMain)GetValue(MediaPlayerProperty); } 
     set { SetValue(MediaPlayerProperty, value); } 
    } 

    public static readonly DependencyProperty MediaPlayerProperty = 
     DependencyProperty.Register(
      "MediaPlayer", typeof(MediaPlayerMain), typeof(PlayList), 
      new FrameworkPropertyMetadata() 
     ); 

}

有隻使用XAML設置MediaPlayer的財產的方式。我試圖使用「{Binding ElementName = MediaPlayerMain}」,但它似乎是MediaPlayerMain尚未初始化。雖然我在InitializeComponent()函數之前初始化它。我究竟做錯了什麼?。將這個對象傳遞給我的用戶控件的最佳選擇是什麼?

+0

是否有MediaPlayerMain類的兩個對象?一個在主窗口中,另一個在usercontrol中?在代碼的第一部分中,您只能在主窗口內創建新對象 – acrilige

+0

不,只有一個MediaPlayerMain對象並且用戶控件應該使用它。這就像委派。 – aleshko

+0

可以請您顯示MediaPlayerMain類。 – ethicallogics

回答

1
public partial class MediaPlayerControlMain : Window,INotifyPropertyChanged 
    { 


     public MediaPlayerControlMain() 
     {     
      InitializeComponent(); 
      MediaPlayerMain = new MediaPlayerMain(); 
     } 
     private MediaPlayerMain mediaPlayerMain; 

     public MediaPlayerMain MediaPlayerMain 
     { 
      get { return mediaPlayerMain; } 
      set { mediaPlayerMain = value; Notify("MediaPlayerMain"); } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void Notify(string propName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(propName)); 

     } 
    } 

"{Binding MediaPlayerMain RelativeSource={RelativeSource AncestorType=Window}}" 

的問題是你想綁定字段不property.For約束力的來源,因爲綁定系統採用反射和只查找不域屬性必須是產權不場。我希望這將有所幫助。

+0

謝謝。你是對的。物業解決了我的問題。 – aleshko

1

您需要命名您的根元素(在窗口/用戶控件本身)的標記。即:

<Window x:Name="mediaPlayer" 
    ....> 
    <TextBlock Text="{Binding SomeProperty,ElementName=mediaPlayer}" 
+0

如果他不使用MVVM,這是最簡單的方法。 – JoanComasFdz

+0

也謝謝。它也很棒。 – aleshko

0

我沒有看到你設置DataContext的任何地方,而且我不認爲你在你的XAML樹命名MediaPlayerMain對象

當你寫{Binding ElementName=MediaPlayerMain},你告訴WPF來設置屬性等於在名爲MediaPlayerMain

視覺樹XAML對象,你可能要找的反而是綁定到一個屬性在DataContext的命名MediaPlayerMain,在這種情況下,你的綁定是這樣的:

<local:PlayList MediaPlayer="{Binding MediaPlayerMain}" /> 

但是,除非您的DataContext設置爲包含屬性MediaPlayerMain的對象,例如在您的Window的構造函數中設置this.DataContext = this,否則這將不起作用。

作爲替代方案,你可以使用ElementName在你的綁定來告訴WPF來查找屬性的對象在可視化樹而不是在DataContext,如您Window

<local:MediaPlayerControlMain x:Name="MainWindow" ...> 
    <local:PlayList MediaPlayer="{Binding MediaPlayerMain, ElementName=MainWindow}" /> 
</local:MediaPlayerControlMain> 

這將使在名爲MainWindow的XAML元素,這是你的MediaPlayerControlMain類物業MediaPlayerMain您綁定的樣子。

如果你是新來WPF的數據綁定,我其實有專爲初學者,應該幫助你更好地理解它是什麼在我的博客的文章,它是如何工作的:What is this "DataContext" you speak of?

+0

謝謝你,蕾切爾。我讀過你的文章,它幫助我更好地理解綁定。 – aleshko