【发布时间】:2016-11-18 15:04:45
【问题描述】:
我见过其他人偶尔会遇到这个问题,我可以说我已经复制了他们修复的几个变体,但还没有让它起作用。
我知道我的绑定数据正在发送正确的 INotify 事件,因为我可以将其他控件绑定到文本块等数据,并在对象属性更改时看到其内容更改,但我的用户控件似乎根本没有接收到事件。
public partial class MappingSelector : UserControl
{
public Type OutputDriver
{
get { return (Type)GetValue(OutputDriverProperty); }
set { Console.WriteLine(value.ToString()); SetValue(OutputDriverProperty, value); UpdateUI(); }
}
// Using a DependencyProperty as the backing store for OutPutDriver. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OutputDriverProperty =
DependencyProperty.Register("OutputDriver", typeof(Type), typeof(MappingSelector), new PropertyMetadata(null));
public MappingSelector()
{
InitializeComponent();
(this.Content as FrameworkElement).DataContext = this;
//UpdateUI();
}
}
setter 有一个永远不会触发的控制台跟踪,所以我确信该属性永远不会被设置。
然后我使用以下方式绑定到它:
<root:MappingSelector OutputDriver="{Binding LoadedProfile.UsedDriverInterface, ElementName=page, UpdateSourceTrigger=PropertyChanged}"/>
而且我知道LoadedProfile.UsedDriverInterface 正在更新并发送正确的事件,因为我也有这个工作正常:
<TextBlock Text="{Binding LoadedProfile.UsedDriverInterface, ElementName=page, UpdateSourceTrigger=PropertyChanged}"/>
后期编辑: 这行得通,但这真的是我需要做的吗?没有更好的办法吗? 将此添加到用户控件构造函数中;
var OutputDriverDPD = DependencyPropertyDescriptor.FromProperty(OutputDriverProperty, typeof(MappingSelector));
OutputDriverDPD.AddValueChanged(this, (sender, args) =>
{
OutputDriver = (Type)GetValue(OutputDriverProperty);
});
【问题讨论】:
标签: c# wpf data-binding dependency-properties