Reputation: 159
I have a Window which uses a UserControl (defined programmatically). My Window uses an Object WindowDatas as DataContext. This object contains an object PJDataContext which defines the Datas for my UserControl (code modularity).
The problem is I can't update the ListView of my UserControl by using PropertyChanged event. However, I know that the binding is correct at initialisation because if my list is not empty, I have something on the screen.
Here is the code : UserControl XAML (simplified, don't take account of the Grid.Row property)
<UserControl x:Class="DandDAdventures.XAML.Controls.PJView"
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="clr-namespace:DandDAdventures.XAML.Controls"
xmlns:db="clr-namespace:DandDAdventures"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding PJDatas}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<ListView Grid.Column="0" ItemsSource="{Binding Path=EventList, UpdateSourceTrigger=PropertyChanged}">
<ListView.Resources>
<DataTemplate DataType="{x:Type db:GroupEvent}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="["/>
<TextBlock Text="{Binding ID}"/>
<TextBlock Text="]"/>
</StackPanel>
</DataTemplate>
</ListView.Resources>
</ListView>
<GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" ></GridSplitter>
<TextBlock Grid.Column="2"></TextBlock>
</Grid>
</Grid>
</UserControl>
UserControl DataContext :
public class PJDataContext : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<GroupEvent> m_groupEvent; //The List
public PJDataContext()
{
m_groupEvent = new List<GroupEvent>();
}
public void AddGroupEvent(GroupEvent ge)
{
m_groupEvent.Add(new GroupEvent { ID = 25 });
m_groupEvent.Add(ge);
this.EventList = m_groupEvent; //Using PropertyChanged
}
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public List<GroupEvent> EventList { get => m_groupEvent; set { m_groupEvent = value; OnPropertyChanged("EventList"); } }
}
The Window DataContext :
public class WindowData : INotifyPropertyChanged
{
protected PJDataContext m_pjDatas;
public event PropertyChangedEventHandler PropertyChanged;
public WindowData()
{
m_pjDatas = new PJDataContext();
}
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public String CurrentPJ
{
get => m_currentPJ;
set
{
m_currentPJ = value;
}
}
public PJDataContext PJDatas { get => m_pjDatas; }
}
Thank you !
Upvotes: 1
Views: 686
Reputation: 128146
While the line
this.EventList = m_groupEvent;
in the AddGroupEvent method fires the PropertyChanged event, the event will be ignored by the EventList Binding, because the underlying collection instance hasn't changed.
You should use an ObservableCollection
for the EventList property, which will also greatly simplify your view model class:
public class PJDataContext
{
public ObservableCollection<GroupEvent> EventList { get; }
= new ObservableCollection<GroupEvent>();
public void AddGroupEvent(GroupEvent ge)
{
EventList.Add(ge);
}
}
Now even the AddGroupEvent method seems redundant.
Upvotes: 1