简体   繁体   English

WPF自定义控件:Collection类型的DependencyProperty

[英]WPF Custom Control: DependencyProperty of Collection type

I have a CustomControl which contains a ListBox : 我有一个包含ListBoxCustomControl

<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>

I bind the ItemsSource with a property in the Code Behind: 我使用Code Behind中的属性绑定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 ) );
            }
        }
    }

Now in my MainWindow I try to bind an ObservableCollection of "Articles" with my CustomControl and its ListSource DependencyProperty: 现在在我的MainWindow我尝试使用CustomControl和它的ListSource DependencyProperty绑定一个ObservableCollection “Articles”:

<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>

And the error I get: 我得到的错误:

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]'

If in the Custom Control I have ObservableCollection<Article> instead of ObservableCollection<object> it works. 如果在自定义控件中我有ObservableCollection<Article>而不是ObservableCollection<object>它可以工作。 So is there a way I can bind my Custom Control's DependencyProperty with an ObservableCollection of objects without having to specify the object's type? 那么有没有办法可以将自定义控件的DependencyProperty与对象的ObservableCollection绑定,而无需指定对象的类型?

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

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

相关问题 自定义控件中Style类型的DependencyProperty - DependencyProperty of type Style in custom control WPF自定义控件:将CollectionViewSource绑定到DependencyProperty - WPF Custom Control: Bind CollectionViewSource to DependencyProperty WPF:DependencyProperty在TextBlock上有效,但不适用于自定义控件 - WPF: DependencyProperty works on TextBlock but not on custom control WPF自定义控件DependencyProperty不会数据绑定 - WPF custom control DependencyProperty won't databind WPF - 自定义控件 - 继承的DependencyProperty和PropertyChangedCallback - WPF – Custom Control – Inherited DependencyProperty and PropertyChangedCallback WPF通过窗口内的自定义用户控件绑定到DependencyProperty问题 - WPF Binding to a DependencyProperty problem with an custom User Control inside a Window WPF 自定义控件,将多个 ComboBox 值组合成单个 DependencyProperty - WPF Custom Control, Combine multiple ComboBox values into single DependencyProperty 自定义控件中的DependencyProperty问题 - Problem with DependencyProperty in custom control 具有DependencyProperty的自定义控件的Designer行为 - Designerbehavior of custom control with DependencyProperty 将自定义控件中的泛型类型 ItemsControl ItemsSource 绑定到 DependencyProperty - Binding generic type ItemsControl ItemsSource in custom control to DependencyProperty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM