0
我見過其他人有這個問題,有時我最好告訴我已經複製了幾個修正的變體,但還沒有得到它的工作。用戶控件上的依賴項屬性綁定數據發生更改時不更新屬性
我知道我的綁定數據正在發送適當的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);
});
原因在這裏解釋:[XAML加載和依賴屬性](https://msdn.microsoft.com/en-us/library/bb613563(v = vs.110).aspx) – Clemens
難題,所以使用DP作爲我所有事件和條件的「樞紐」,並且只是使用屬性來控制綁定? – Wobbles
@Wobbles是的,屬性獲取/設置僅供您使用,框架將使用DP。 – nvoigt