绑定到在Silverlight字典与INotifyPropertyChanged的 [英] Binding to a dictionary in Silverlight with INotifyPropertyChanged

查看:102
本文介绍了绑定到在Silverlight字典与INotifyPropertyChanged的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight中,我不能让INotifyPropertyChanged的工作,就像我想它的时候绑定到字典。在下面的例子中,页面绑定到字典还好,但是当我改变文本框之一的内容CustomProperties来属性setter不叫。当CustomProperties来设置的CustomProperties来属性setter时,才会调用,而不是在它的值设置时。我试图做的字典值一些验证,因此我期待运行约code当字典中的每个值被改变。难道有什么我可以在这里做什么?

C#

 公共部分MainPage的类:用户控件
{

    MainPage的公()
    {
        的InitializeComponent();

        myEntity所ENT =新myEntity所();
        ent.CustomProperties.Add(标题,先生);
        ent.CustomProperties.Add(名字,约翰);
        ent.CustomProperties.Add(姓名,史密斯);

        this.DataContext =耳鼻喉科;
    }

}

公共类myEntity所:INotifyPropertyChanged的
{

    公共事件PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
    公共委托无效PropertyChangedEventHandler(对象发件人,System.ComponentModel.PropertyChangedEventArgs E);

    私人字典<字符串,对象> _customProps;
    公共字典<字符串,对象> CustomProperties来{
        得到 {
            如果(_customProps == NULL){
                _customProps =新字典<字符串,对象>();
            }
            返回_customProps;
        }
        组 {
            _customProps =价值;
            如果(的PropertyChanged!= NULL){
                的PropertyChanged(这一点,新PropertyChangedEventArgs(CustomProperties来));
            }
        }
    }

}
 

VB

 部分公共MainPage的类
    继承用户控件

    公共子新()
        的InitializeComponent()

        昏暗的耳鼻喉科作为新myEntity所
        ent.CustomProperties.Add(标题,先生)
        ent.CustomProperties.Add(名字,约翰)
        ent.CustomProperties.Add(姓名,史密斯)

        Me.DataContext = ENT
    结束小组

末级

公共类myEntity所
    实现INotifyPropertyChanged

    公共事件的PropertyChanged(BYVAL发件人为对象,BYVAL E上System.ComponentModel.PropertyChangedEventArgs)实现System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    私人_customProps作为词典(字符串,对象)
    公共属性CustomProperties来作为词典(字符串,对象)
        得到
            如果_customProps是没有那么
                _customProps =新词典(字符串,对象)
            结束如果
            返回_customProps
        最终获取
        设置(BYVAL值作为词典(字符串,对象))
            _customProps =价值
            的RaiseEvent的PropertyChanged(ME,新PropertyChangedEventArgs(CustomProperties来))
        结束设定
    高端物业

末级
 

XAML中

 <文本框高度=23NAME =TextBox1的文本={绑定路径= CustomProperties来[标题],模式=双向}/>
<文本框高度=23NAME =TextBox2中文本={绑定路径= CustomProperties来[名字],模式=双向}/>
<文本框高度=23NAME =TextBox3文本={绑定路径= CustomProperties来[名],模式=双向}/>
 

解决方案

一个收集需要实现<一个href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.inotifycollectionchanged%28VS.95%29.aspx">INotifyCollectionChanged除了<一个介面 href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28VS.95%29.aspx">INotifyPropertyChanged接口以支持数据绑定。该的ObservableCollection类实现他们的名单,喜欢收藏,但我相信这是在.NET Framework,这是否没有字典的集合。你可能不得不自己实现了。

In silverlight, I can not get INotifyPropertyChanged to work like I want it to when binding to a dictionary. In the example below, the page binds to the dictionary okay but when I change the content of one of the textboxes the CustomProperties property setter is not called. The CustomProperties property setter is only called when CustomProperties is set and not when the values within it are set. I am trying to do some validation on the dictionary values and so am looking to run some code when each value within the dictionary is changed. Is there anything I can do here?

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

解决方案

A collection needs to implement the INotifyCollectionChanged interface in addition to the INotifyPropertyChanged interface to support data binding. The ObservableCollection class implements them for a List-like collection, but I believe there is no dictionary-like collection in the .NET Framework that does this. You likely have to implement it yourself.

这篇关于绑定到在Silverlight字典与INotifyPropertyChanged的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆