2013-09-24 53 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。