В silverlight я не могу заставить INotifyPropertyChanged работать так, как я хочу, при привязке к словарю. В приведенном ниже примере страница привязывается к словарю в порядке, но когда я изменяю содержимое одного из текстовых полей, свойство SetProperties не вызывается. Настройщик свойств CustomProperties вызывается только при настройке CustomProperties, а не при установке значений внутри него. Я пытаюсь сделать некоторые проверки на значения словаря, и поэтому я хочу запустить некоторый код, когда изменяется каждое значение в словаре. Я могу здесь что-нибудь сделать?Связывание со словарем в Silverlight с INotifyPropertyChanged
C#
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
MyEntity ent = new MyEntity();
ent.CustomProperties.Add("Title", "Mr");
ent.CustomProperties.Add("FirstName", "John");
ent.CustomProperties.Add("Name", "Smith");
this.DataContext = ent;
}
}
public class MyEntity : INotifyPropertyChanged
{
public event PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
public delegate void PropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e);
private Dictionary<string, object> _customProps;
public Dictionary<string, object> CustomProperties {
get {
if (_customProps == null) {
_customProps = new Dictionary<string, object>();
}
return _customProps;
}
set {
_customProps = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("CustomProperties"));
}
}
}
}
В.Б.
Partial Public Class MainPage
Inherits UserControl
Public Sub New()
InitializeComponent()
Dim ent As New MyEntity
ent.CustomProperties.Add("Title", "Mr")
ent.CustomProperties.Add("FirstName", "John")
ent.CustomProperties.Add("Name", "Smith")
Me.DataContext = ent
End Sub
End Class
Public Class MyEntity
Implements INotifyPropertyChanged
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Private _customProps As Dictionary(Of String, Object)
Public Property CustomProperties As Dictionary(Of String, Object)
Get
If _customProps Is Nothing Then
_customProps = New Dictionary(Of String, Object)
End If
Return _customProps
End Get
Set(ByVal value As Dictionary(Of String, Object))
_customProps = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CustomProperties"))
End Set
End Property
End Class
Xaml
<TextBox Height="23" Name="TextBox1" Text="{Binding Path=CustomProperties[Title], Mode=TwoWay}" />
<TextBox Height="23" Name="TextBox2" Text="{Binding Path=CustomProperties[FirstName], Mode=TwoWay}" />
<TextBox Height="23" Name="TextBox3" Text="{Binding Path=CustomProperties[Name], Mode=TwoWay}" />
Спасибо, я tweeked реализация dr wpf в ObservableDictionary: http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/ –