Csharp/C#教程:使用INotifyPropertyChanged绑定到Silverlight中的字典分享


使用INotifyPropertyChanged绑定到Silverlight中的字典

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

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 _customProps; public Dictionary CustomProperties { get { if (_customProps == null) { _customProps = new Dictionary(); } 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

    

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

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

 public class MyEntity : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private readonly Dictionary _customProps = new Dictionary(); 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: –

     

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

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

上述就是C#学习教程:使用INotifyPropertyChanged绑定到Silverlight中的字典分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/955307.html

(0)
上一篇 2021年11月21日
下一篇 2021年11月21日

精彩推荐