c# - 双向数据绑定(bind)、Silverlight 和自定义控件/依赖属性

标签 c# silverlight

如果我在采用 IEnumerable<T> 的自定义控件上创建依赖属性.

例如 IEnumerable<string> :

public static readonly DependencyProperty MyCollectionProperty =
    DependencyProperty.Register("MyCollection", typeof(IEnumerable<string>), typeof(MyControl), new PropertyMetadata(new List<string>()));

public IEnumerable<string> MyCollection
{
     get { return (IEnumerable<string>)GetValue(MyCollectionProperty); }
     set { SetValue(MyCollectionProperty, value); }
}

如果我数据绑定(bind) ObservableCollection<T><string>在这种情况下。 Silverlight 是否为我处理双向数据绑定(bind)?

最佳答案

来自 MSDN “特别是,如果您正在使用 OneWay 或 TwoWay(例如,您希望在源属性动态更改时更新 UI),则必须实现合适的属性更改通知机制,例如 INotifyPropertyChanged 接口(interface)”

ObservableCollection<T>为您实现 INotifyPropertyChanged。 IEnumerable<T>才不是。如果您想要简单的双向绑定(bind),只需绑定(bind)到 ObservableCollection<T> , 并更改您的 UpdateSourceTriggerPropertyChanged . XAML:

<ItemSource = "{Binding Path=MyCollection, UpdateSourceTrigger=PropertyChanged}">

关于c# - 双向数据绑定(bind)、Silverlight 和自定义控件/依赖属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4016303/

相关文章:

silverlight - 当我处理 MouseLeave 事件时找出我的鼠标在哪里?

C# NHibernate 简单问题

C# 泛型 - 设计没有下限?

c# - WPF 中 ContextMenu 中的 CommandParameters

silverlight - Windows Phone 是否延迟或批处理 HTTP 请求?看到对同一 LAN 上的服务器的 600 毫秒请求延迟

Silverlight WCF 与 BasicHttpBinding 配合使用,使用 CustomBinding/BinaryMessageEncoding 时出现 "HTTP 415 Unsupported Media Type"异常

flash - 我可以在 silverlight 应用程序中包含 Flash 内容吗?

c# - 使 VirtualizingStackPanel 不清理已加载的项目

c# - 在 WPF 媒体元素中连续重播视频

c# - 使用 Take() 从字节数组中获取字节,在 Take() 之后无法转换回 byte[]