繁体   English   中英

使用INotifyPropertyChanged绑定到Silverlight中的字典

[英]Binding to a dictionary in Silverlight with 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"));
            }
        }
    }

}

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

这是一个(有点过于简化)的解决方案。

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

主页:-

    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。

除了INotifyPropertyChanged接口之外,集合还需要实现INotifyCollectionChanged 接口以支持数据绑定。 ObservableCollection类为类似List的集合实现它们,但我相信.NET Framework中没有类似字典的集合来执行此操作。 您可能必须自己实施它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM