ItemsSource binding to a list seems to be working fine except for when inside a DataTemplate. I have 3 ComboBoxes right now: 2 which are inside a DataTemplate, where one of them has hardcoded items which are working and one which has ItemsSource set which is not working. The last is outside a DataTemplate and is working with an ItemsSource. 2 lists of ComboBoxes and 1 ComboBox
I have tried to modify the DataContext, RelativeSource, and ElementName, but without any luck.
The ItemsSource list contains ListEntry
public class ListEntry : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string m_name;
private string m_desc;
public ListEntry(string name, string desc)
{
Name = name;
Desc = desc;
}
public string Name
{
get { return m_name; }
set { m_name = value; NotifyPropertyChanged("Name"); }
}
public string Desc
{
get { return m_desc; }
set { m_desc = value; NotifyPropertyChanged("Desc"); }
}
}
This is my DataContext
public class DataClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<ListEntry> m_itemsList;
public ObservableCollection<ListEntry> ItemsList
{
get { return m_itemsList; }
set { m_itemsList = value; NotifyPropertyChanged("ItemsList"); }
}
}
This is the ComboBox with ItemsSource
XAML
<Window.Resources>
<DataTemplate x:Key="DataTempItemsSource">
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding ItemsList}" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedIndex="0"/>
</DataTemplate>
<ListBox HorizontalAlignment="Center"
VerticalAlignment="Center"
ItemTemplate="{StaticResource DataTempItemsSource}"
ItemsSource="{Binding ItemsList}">
</ListBox>
This is the ComboBox with hardcoded values which are working as it should
XAML
<DataTemplate x:Key="DataTempHardCode">
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" SelectedIndex="0">
<ComboBoxItem Content="One"/>
<ComboBoxItem Content="Two"/>
</ComboBox>
</DataTemplate>
<ListBox HorizontalAlignment="Center"
VerticalAlignment="Center"
ItemTemplate="{StaticResource DataTempHardCode}"
ItemsSource="{Binding ItemsList}">
</ListBox>
I have also confirmed that the ComboBox with ItemsSource is working outside the DataTemplate.
<ComboBox HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding ItemsList}" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedIndex="0"/>
I get theese two errors: System.Windows.Data Error: 40 : BindingExpression path error: 'ItemsList' property not found on 'object' ''ListEntry' (HashCode=20917673)'. BindingExpression:Path=ItemsList; DataItem='ListEntry' (HashCode=20917673); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Error: 40 : BindingExpression path error: 'ItemsList' property not found on 'object' ''ListEntry' (HashCode=52252659)'. BindingExpression:Path=ItemsList; DataItem='ListEntry' (HashCode=52252659); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
Any ideas what is wrong? My other bindings are working so I don´t think the DataContext is wrong (it is set in the MainWindow.cs file: DataContext = this.dataClass;)