Dictionary binding in Silverlight using the INotifyPropertyChanged function

In silverlight, I cannot get INotifyPropertyChanged to work as I want when binding to a dictionary. In the example below, the page is bound to the dictionary in order, but when I change the contents of one of the text fields, the SetProperties property is not called. The CustomProperties property adjuster is called only when CustomProperties is configured, and not when setting values ​​inside it. I am trying to do some checks on the values ​​of a dictionary, and therefore I want to run some code when each value in the dictionary changes. Can I do anything here?

FROM#

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")); } } } } 

B. B.

 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}" /> 
+4
source share
2 answers

Collections need to implement the INotifyCollectionChanged interface in addition to the INotifyPropertyChanged interface to support data binding. The ObservableCollection class implements them for a collection like List, but I find that in the .NET Framework this is not like a dictionary. You may have to implement it yourself.

+6
source

Here is one (somewhat simplified) solution.

 public class MyEntity : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private readonly Dictionary<string, object> _customProps = new Dictionary<string, object>(); public void AddCustomProperty(string key, object value) { _customProps.Add(key, value); } public object this[string key] { get { return _customProps[key]; } set { // The validation code of which you speak here. _customProps[key] = value; NotifyPropertyChanged("Item[" + key "]"); } } private void NotifyPropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } } 

MainPage: -

  public MainPage() { InitializeComponent(); MyEntity ent = new MyEntity(); ent.AddCustomProperty("Title", "Mr"); ent.AddCustomProperty("FirstName", "John"); ent.AddCustomProperty("Name", "Smith"); this.DataContext = ent; } 

MainPage Xaml: -

  <TextBox Height="23" Name="TextBox1" Text="{Binding [Title], Mode=TwoWay}" /> <TextBox Height="23" Name="TextBox2" Text="{Binding [FirstName], Mode=TwoWay}" /> <TextBox Height="23" Name="TextBox3" Text="{Binding [Name], Mode=TwoWay}" /> 

The important thing for your problem is that your code is involved in assigning a property value in the dictionary. Your corrected code showed Dictionary , all tasks were performed through the infrastructure component code. In this version of MyEntity there is a string indexer where user properties are assigned directly and the Dictionary is made private.

Note that running INotifyPropertyChanged not strictly necessary to answer your specific question, but I suspect you will need it. If, for example, you reassigned a new value to a custom property, you need the notification of the user interface.

+8
source

Source: https://habr.com/ru/post/1306903/


All Articles