Search code examples
c#wpfxamlbindingitemssource

ItemsSource Binding in UserControl doens't work


i am currently testing UserControls and therefore created this little App.

Main.xaml

<Grid>
<control:CustomInterfaceGrid Color="Green" Height="400" CustomItemsSource="{Binding Packages}"></control:CustomInterfaceGrid>
</Grid>

UserControl.xaml

<UserControl x:Class="App.Custom.CustomInterfaceGrid"
             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" 
             xmlns:local="App.Custom"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             x:Name="SourceElement"
             >
        <Grid>
        <ListBox x:Name="listView" Background="{Binding Color, ElementName=SourceElement}" ItemsSource="{Binding CustomItemsSource, ElementName=SourceElement}"></ListBox>
    </Grid>
</UserControl>

CodeBehind from UserControl

public partial class CustomInterfaceGrid : UserControl, INotifyPropertyChanged
  {

     public CustomInterfaceGrid()
    {
      InitializeComponent();
      DataContext = this;
     }

    public static readonly DependencyProperty ColorProperty =

    DependencyProperty.Register("Color", typeof(SolidColorBrush), typeof(CustomInterfaceGrid));


    public SolidColorBrush Color
    {
      get; set;
    }


    public static readonly DependencyProperty CustomItemsSourceProperty =

    DependencyProperty.Register("CustomItemsSource", typeof(IEnumerable<Object>), typeof(CustomInterfaceGrid));

    public IEnumerable<Object> CustomItemsSource
    {
      get
      {
        return GetValue(CustomItemsSourceProperty) as IEnumerable<Object>;
      }
      set {
        SetValue(CustomItemsSourceProperty, value);
        OnPropertyChanged();
      }
    }
    public event PropertyChangedEventHandler PropertyChanged;


    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }
    }

  }

The problem is that my the color changes when I set it in Main, but it wont show the Packages from the List. When i bind the Packages directly to a list in Main.xaml its alright. So the fault has to be somewhere else. Hope you can help!


Solution

  • the main mistakes which leads to binding erros is unnecessary set DataContext. remove this line from constructor:

     DataContext = this;
    

    there is no need to implement INotifyPropertyChanged for UserControl which is also DependencyObject. DependencyProperties have internal mechanism to notify about changes. Remove OnPropertyChanged - declaration and all usages.

    Speaking of DependencyProperties: public SolidColorBrush Color { get; set; } doesn't follow required pattern and has to be implemented with GetValue / SetValue method