我有一个名为InformationControl的用户控件。我在我的窗口中使用它,就像这样:
<general:InformationControl Grid.Row="0" TimeToStart="{Binding TimeToStart}" Poor="{Binding Path=Mine.Poor}" Status="{Binding Path=Mine.MineStatus}"/>
TimeToStart依赖项属性上的绑定工作正常,但是当我使用Path=Object.Property
时似乎不起作用。
但是,当我在常规控件(即不是UserControl)上使用Path=Object.Property
时,它可以正常工作:
<ItemsControl Grid.Row="2" Name="Items" ItemsSource="{Binding Path=Mine.Participants}">
我在Mine的getter中打破了,并且在绑定日志记录中它说它是null,但它确实被设置了。此外,它试图首先获得该属性的事实让我觉得绑定是正确的,但我无法弄清楚为什么它不起作用。
我是否需要执行不同的操作以确保嵌套属性的绑定适用于UserControl依赖项属性?
答案 0 :(得分: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并直接调用setvalue。