WPF自定义控件:集合类型的DependencyProperty

发布于 2024-11-01 03:21:12 字数 3280 浏览 1 评论 0原文

我有一个包含 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>

我将 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< /code> 我尝试将“Articles”的 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

而不是 ObservableCollection它有效。 那么有没有一种方法可以将自定义控件的 DependencyProperty 与对象的 ObservableCollection 绑定在一起,而无需指定对象的类型?

I have a CustomControl which contains a 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>

I bind the ItemsSource with a property in the Code Behind:

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟火散人牵绊 2024-11-08 03:21:12

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

Change type of ListSource to IEnumerable, then you can bind to any collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文