2010-04-14 47 views
4

在Silverlight中,我無法讓INotifyPropertyChanged像綁定到字典時那樣工作。在下面的例子中,頁面綁定到字典好,但是當我更改其中一個文本框的內容時,不會調用CustomProperties屬性設置器。 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")); 
      } 
     } 
    } 

} 

VB

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}" /> 

回答

8

這是一個(有點過於簡單化)的解決方案。

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}" /> 

針對您的問題,重要的是你的代碼中包含的屬性值到字典中的分配。原來你的代碼暴露了Dictionary,所有的分配工作都經過了框架組件代碼。在此版本中,MyEntity具有字符串索引器,其中自定義屬性直接分配並且Dictionary被設置爲私有。

注意INotifyPropertyChanged的執行並不是嚴格需要回答你的具體問題,但我懷疑你會需要它。例如,如果你重新分配一個新的值給一個自定義屬性,你會希望UI被通知。

相關問題