I created a own view called StdListView, which contains a Usercontrol called AutoGeneratedFilterBar. I want to "pass" a List to the AutoGeneratedFilterBar-Control via Data-Binding. But this didn't work. Binding error:
System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''AutoGeneratedFilterBar' (Name='')'. BindingExpression:Path=Items; DataItem='AutoGeneratedFilterBar' (Name=''); target element is 'AutoGeneratedFilterBar' (Name=''); target property is 'ItemsSource' (type 'ObservableCollection`1')
I understand the error to mean that the user control is looking for a property called Items in its own data-context.
Here is my Code: The XAML-Code of the View, which contains the Usercontrol:
<Window x:Class="MarketingTool.UI.StandardGUIViews.View.StdListView"
...
xmlns:ctrl="clr-namespace:MarketingTool.UI.Controls"
mc:Ignorable="d"
Title="StandardListView" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<ctrl:AutoGeneratedFilterBar Grid.Row="0" ItemsSource="{Binding Items}"></ctrl:AutoGeneratedFilterBar>
</Grid>
Code-behind of this view:
public partial class StdListView : Window
{
public StdListView()
{
InitializeComponent();
}
}
ViewModel of this view:
public class UserListViewModel : DependencyObject, INotifyPropertyChanged{
public ObservableCollection<User> Items
{
get
{
return _items;
}
set
{
if (value != _items)
{
_items = value;
OnPropertyChanged("Items");
}
}
}
public UserListViewModel(Window currentWindow) : base(currentWindow)
{
LoadItems();
}
protected void LoadItems()
{
UserRepository userRepository = new UserRepository();
Items = new ObservableCollection<User>(userRepository.List());
}
private Window currentWindow;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Call of the Window with the usercontrol:
StdListView stdListView = new StdListView();
UserListViewModel userListViewModel = new UserListViewModel(stdListView);
stdListView.DataContext = userListViewModel;
stdListView.Show();
And here comes the usercontrol-xaml (There is currently nothing in the xaml, as I only need the item source data later in the code.):
<UserControl x:Class="MarketingTool.UI.Controls.AutoGeneratedFilterBar"
...
xmlns:local="clr-namespace:MarketingTool.UI.Controls"
d:DesignHeight="450" d:DesignWidth="800">
</UserControl.Resources>
<Grid x:Name="FilterbarLayoutRoot" >
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" >
...
</ScrollViewer>
</Grid>
</UserControl>
UserControl code-behind:
public partial class AutoGeneratedFilterBar : UserControl
{
public ObservableCollection<User> ItemsSource
{
get { return (ObservableCollection<User>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<User>), typeof(AutoGeneratedFilterBar), new PropertyMetadata(default(ObservableCollection<User>)));
public AutoGeneratedFilterBar()
{
DataContext = this;
InitializeComponent();
var ItemsSourcePropertyDescriptor = DependencyPropertyDescriptor.FromProperty(AutoGeneratedFilterBar.ItemsSourceProperty, typeof(AutoGeneratedFilterBar));
tp.AddValueChanged(this, ItemsSourcePropertyChanged);
}
private void ItemsSourcePropertyChanged(object sender, EventArgs e)
{
int i = 0;
}
}