使用INotifyPropertyChanged绑定到Silverlight中的字典

Car*_*pon 4 .net c# data-binding silverlight inotifypropertychanged

在Silverlight中,我无法让INotifyPropertyChanged在绑定到字典时像我想要的那样工作.在下面的示例中,页面可以正常绑定到字典,但是当我更改其中一个文本框的内容时,不会调用CustomProperties属性setter.CustomProperties属性设置器仅在设置CustomProperties时调用,而不是在设置其中的值时调用.我试图对字典值进行一些验证,所以我希望在字典中的每个值都改变时运行一些代码.我能在这做什么吗?

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

}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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}" />
Run Code Online (Sandbox Code Playgroud)

Ant*_*nes 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));
    }
}
Run Code Online (Sandbox Code Playgroud)

主页:-

    public MainPage()
    {
        InitializeComponent();

        MyEntity ent = new MyEntity();
        ent.AddCustomProperty("Title", "Mr");
        ent.AddCustomProperty("FirstName", "John");
        ent.AddCustomProperty("Name", "Smith");

        this.DataContext = ent;

    }
Run Code Online (Sandbox Code Playgroud)

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}" />
Run Code Online (Sandbox Code Playgroud)

对于您的问题,重要的是您的代码涉及将属性值分配到字典中.最初你的代码公开了Dictionary,所有的赋值工作都经过了框架组件代码.在这个版本中MyEntity有一个字符串索引器,其中自定义属性是直接分配的,并且Dictionary是私有的.

请注意INotifyPropertyChanged,回答您的具体问题并非绝对必要的实施,但我怀疑您需要它.例如,如果您将新值重新分配给自定义属性,则需要通知UI.


dtb*_*dtb 6

除了INotifyPropertyChanged接口之外,集合还需要实现INotifyCollectionChanged 接口以支持数据绑定.的的ObservableCollection类实现他们的列表类似的集合,但我相信这是在做这个.NET Framework中没有字典的集合.您可能必须自己实施它.