【问题标题】:WPF Custom Control: DependencyProperty of Collection typeWPF 自定义控件:集合类型的 DependencyProperty
【发布时间】:2011-04-14 13:35:55
【问题描述】:

我有一个CustomControl,其中包含一个ListBox:

<UserControl x:Class="WpfApplication1.CustomList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <ListBox Name="listBox1" ItemsSource="{Binding ListSource}" />
    </Grid>
</UserControl>

我将ItemsSource 与代码背后的属性绑定:

public partial class CustomList : UserControl, INotifyPropertyChanged
    {
        public CustomList( )
        {
            InitializeComponent( );
        }

        public ObservableCollection<object> ListSource
        {
            get
            {
                return (ObservableCollection<object>)GetValue( ListSourceProperty );
            }
            set
            {
                base.SetValue(CustomList.ListSourceProperty, value);
                NotifyPropertyChanged( "ListSource" );
            }
        }

        public static DependencyProperty ListSourceProperty = DependencyProperty.Register(
             "ListSource",
             typeof( ObservableCollection<object> ),
             typeof( CustomList ),
             new PropertyMetadata( OnValueChanged ) );

        private static void OnValueChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            ( (CustomList)d ).ListSource = (ObservableCollection<object>)e.NewValue;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged( string propertyName )
        {
            if(PropertyChanged != null)
            {
                PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
            }
        }
    }

现在在我的MainWindow 中,我尝试将“文章”的ObservableCollection 与我的CustomControl 及其ListSource DependencyProperty 绑定:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <local:CustomList ListSource="{Binding Articles}"/>
    </Grid>
</Window>

我得到的错误:

Error: 1 : Cannot create default converter to perform 'one-way' conversions between types 'System.Collections.ObjectModel.ObservableCollection`1[WpfApplication1.Article]' and 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]'

如果在自定义控件中我有 ObservableCollection&lt;Article&gt; 而不是 ObservableCollection&lt;object&gt; 它可以工作。 那么有没有一种方法可以将自定义控件的 DependencyProperty 与对象的 ObservableCollection 绑定,而无需指定对象的类型?

【问题讨论】:

  • 您不需要(实际上不应该)在 DependencyProperties 上实现 INotifyPropertyChanged,也不要将任何代码放在依赖项属性的设置器中。
  • @H.B.您是说依赖属性会自动实现正确的更改通知吗?或者,您是否建议有更好的选择?
  • @UB3571:是的,它们确实实现了更改通知,但它们引入了线程关联。正如我所说,另一种选择是INotifyPropertyChanged。
  • (作为其他人的资源)关于创建自定义依赖项的内容。道具:我通过阅读this link 解决了这个问题,它完整地讨论了自定义依赖属性,this article 讨论了线程亲和性、线程模型以及如何使用 Dispatcher 来发挥你的优势。 (@H.B. 你似乎与整个 INotifyPropertyChanged cmets 有点矛盾......我仍然真的不知道你试图提出什么建议......对不起。)
  • 很简单:依赖属性不需要 INotifyPropertyChanged,因为它们有一个固有的 SetValue 方法,通过设置标识符设置的本地值已经在这里完成了它的工作。没有其他东西订阅 PropertyChanged 事件,那么为什么要引发一个不是特定于线程的事件呢?我感谢您的评论回复,但与其告诉人们如何做以表明您知道改进,请通过在您的陈述背后包含因果推理来帮助我们改进自己:) 如果您不介意。一旦你理解它,一切都“相当简单”

标签: c# wpf custom-controls dependency-properties


【解决方案1】:

将 ListSource 的类型更改为 IEnumerable,然后您可以绑定到任何集合。

【讨论】:

  • 感谢您的快速回复,您完全拯救了这一天。
  • 根据这篇博客文章,您应该使用 IList,这样您也可以在 throwed xaml 中进行填充。 sshumakov.com/2012/11/13/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
相关资源
最近更新 更多