C# 用户控件绑定不适用于嵌套属性
我有一个名为InformationControl的用户控件。我在我的窗口中使用它,如下所示:C# 用户控件绑定不适用于嵌套属性,c#,wpf,data-binding,user-controls,dependency-properties,C#,Wpf,Data Binding,User Controls,Dependency Properties,我有一个名为InformationControl的用户控件。我在我的窗口中使用它,如下所示: <general:InformationControl Grid.Row="0" TimeToStart="{Binding TimeToStart}" Poor="{Binding Path=Mine.Poor}" Status="{Binding Path=Mine.MineStatus}"/> 我正在打断我的getter,在绑定日志中它说它是null,但它肯定已经设置好了。另外,它试
<general:InformationControl Grid.Row="0" TimeToStart="{Binding TimeToStart}" Poor="{Binding Path=Mine.Poor}" Status="{Binding Path=Mine.MineStatus}"/>
我正在打断我的getter,在绑定日志中它说它是null,但它肯定已经设置好了。另外,它试图首先获得属性的事实让我觉得绑定是正确的,但我无法理解为什么它不起作用
是否需要执行其他操作以确保嵌套属性的绑定在UserControl依赖项属性上工作?为信息控制编写代码,在Debug.Print(“newvalue…”)处设置断点
我真的不知道你想实现什么,但是如果你看看我的示例,如果你在InformationControl的依赖属性更改回调中设置断点,那么在用户控件中获取我的.Name是有效的。还请注意,由于clr绕过setter并直接调用setvalue,因此从未调用setter。为信息控制编写代码,请在Debug.Print(“newvalue…”)处设置断点
我真的不知道你想实现什么,但是如果你看看我的示例,如果你在InformationControl的依赖属性更改回调中设置断点,那么在用户控件中获取我的.Name是有效的。还要注意,永远不会调用setter,因为clr绕过setter,直接调用setvalue。InformationControl的DataContext是什么?我很困惑。在ItemsControl中,您尝试了我的。参与者,TimeToStart在哪里?未设置InformationControl的DataContext。我不认为这对于依赖属性是必要的?关于ItemsControl,我想说的一点是Path=Object.Property正在工作,但它对用户控件不起作用。谢谢您能检查设置属性的顺序以及您的用户控件何时使用我的.Property吗?您可能是在用户控件开始加载后才尝试设置它。InformationControl的DataContext是必需的,并且您希望该对象的DataContext上有一个名为“我”的属性,那么您可以执行嵌套属性。@gavin在用户控件加载后尝试设置它并不重要已经开始加载,因为他正在进行绑定,所以它将更新值。InformationControl的DataContext是什么?我很困惑。在ItemsControl中,您尝试了我的。参与者,TimeToStart在哪里?未设置InformationControl的DataContext。我不认为这对于依赖属性是必要的?关于ItemsControl,我想说的一点是Path=Object.Property正在工作,但它对用户控件不起作用。谢谢您能检查设置属性的顺序以及您的用户控件何时使用我的.Property吗?您可能是在用户控件开始加载后才尝试设置它。InformationControl的DataContext是必需的,并且您希望该对象的DataContext上有一个名为“我”的属性,那么您可以执行嵌套属性。@gavin在用户控件加载后尝试设置它并不重要已经开始加载,因为他正在进行绑定,所以它将更新值。
<ItemsControl Grid.Row="2" Name="Items" ItemsSource="{Binding Path=Mine.Participants}">
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);
}
}
}
<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;
}
}