2013-09-24 49 views
1

我有一個名爲InformationControl的用戶控件。我使用它在我的窗口,像這樣:用戶控制綁定不適用於嵌套屬性

<general:InformationControl Grid.Row="0" TimeToStart="{Binding TimeToStart}" Poor="{Binding Path=Mine.Poor}" Status="{Binding Path=Mine.MineStatus}"/> 

工作正常的上TimeToStart依賴屬性的結合,但是當我使用Path=Object.Property是似乎並沒有工作。

然而,當我在一個普通的控制(即,不是用戶控件)使用Path=Object.Property然後正常工作:

<ItemsControl Grid.Row="2" Name="Items" ItemsSource="{Binding Path=Mine.Participants}"> 

我打破了吸礦井,並在綁定日誌記錄跟它它是空的,但它已經確定。另外它試圖首先獲得財產的事實讓我覺得綁定是正確的,但我無法弄清楚它爲什麼不起作用。

我是否需要做一些不同的事情來確保嵌套屬性的綁定適用於UserControl依賴項屬性?

+0

什麼是您的InformationControl的DataContext?我很困惑。在ItemsControl的工作方式中,你嘗試了Mine.Participants,TimeToStart在哪裏? –

+0

DataContext for InformationControl未設置。我不認爲這對於依賴屬性是必要的嗎?關於ItemsControl的一點是Path = Object.Property正在工作,但它不適用於用戶控件。謝謝! – Sherlock

+0

您可以檢查設置屬性的順序以及用戶控件使用Mine.Property的時間嗎?在您的用戶控件已經開始加載後,您可能正在設置它。 – gavin

回答

1

後面的代碼信息的控制,在Debug.Print設置一個斷點( 「NEWVALUE ...」)

namespace WpfStackoverflow 
{ 
    /// <summary> 
    /// Interaction logic for InformationControl.xaml 
    /// </summary> 
    public partial class InformationControl : UserControl 
    { 
     public static readonly DependencyProperty TimeToStartProperty; 
     static InformationControl() 
     { 
      //FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(""); 
      TimeToStartProperty = DependencyProperty.Register("TimeToStart", typeof(string), typeof(InformationControl), new UIPropertyMetadata(string.Empty, UsernamePropertyChangedCallback)); 
     } 

     public string TimeToStart 
     { 
      get { 
       return (string)GetValue(TimeToStartProperty); 
      } 
      set { 
       SetValue(TimeToStartProperty, value); 
      } 
     } 

     public InformationControl() 
     { 
      InitializeComponent(); 
      string temp = TimeToStart; 

     } 
     private static void UsernamePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      Debug.Print("OldValue: {0}", e.OldValue); 
      Debug.Print("NewValue: {0}", e.NewValue); 
     } 
    } 
} 

主窗口XAML:

<Window x:Class="WpfStackoverflow.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfStackoverflow" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <local:InformationControl TimeToStart="{Binding Mine.Name}" /> 

    </Grid> 
</Window> 

主窗口後面的代碼:

namespace WpfStackoverflow 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      Parent p = new Parent(); 
      p.Mine = new Mine(); 
      p.Mine.Name = "Hello world"; 
      this.DataContext = p; 
     } 
    } 
} 

父級:

namespace WpfStackoverflow 
{ 


    public class Parent:INotifyPropertyChanged 
    { 
     private Mine _mine; 
     public Mine Mine 
     { 
      get 
      { 
       return _mine; 
      } 
      set 
      { 
       _mine = value; 
       NotifyPropertyChanged(); 
      } 
     } 
     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 
    public class Mine : INotifyPropertyChanged 
    { 
     private string _name; 
     public string Name { get { return _name; } 
      set 
      { 
       _name = value; 
       NotifyPropertyChanged(); 
      } 
     } 
     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 
    } 

} 

我真的不知道你在做什麼,但是如果你看看我的例子,如果你在InformationControl的依賴屬性改變回調中設置了一個斷點,Mine.Name在用戶控件中工作。還要注意,setter永遠不會被調用,因爲clr會直接繞過setter和call setvalue。