Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Custom Control: DependencyProperty of Collection type

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?

like image 849
Aris Avatar asked Apr 14 '11 13:04

Aris


1 Answers

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

like image 113
Jess Panni Avatar answered Sep 21 '22 17:09

Jess Panni