I have two WPF components, one is containing a list of the other one.
I set the DataContext in the parent WPF component, so I can define a property.
Unfortunately, the PropertyChanged
of my model is always null, therefore my label (computed property) is not updated.
The component containing the view:
<Grid x:Name="ValidationGrid" Background="White">
<subView:SingleStepView x:Name="Step01" Grid.Column="0" Grid.Row="0" >
<subView:SingleStepView.DataContext>
<subView:SingleStepModel CurrentStep="Step1"/>
</subView:SingleStepView.DataContext>
</subView:SingleStepView>
<subView:SingleStepView x:Name="Step02" Grid.Column="1" Grid.Row="0" >
<subView:SingleStepView.DataContext>
<subView:SingleStepModel CurrentStep="Step2"/>
</subView:SingleStepView.DataContext>
</subView:SingleStepView>
</Grid>
The View with the label:
<UserControl x:Name="SingleStep" x:Class="MyNameSpace.Views.SubView.SingleStepView"
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:MyNameSpace.Views.SubView"
mc:Ignorable="d"
d:DesignHeight="70" d:DesignWidth="146" MouseDoubleClick="SingleStep_MouseDoubleClick">
<UserControl.Resources>
<local:SingleStepModel x:Key="SingleStepModel" />
</UserControl.Resources>
<Grid x:Name="SingleStepGrid" DataContext="{StaticResource SingleStepModel}">
<Label x:Name="LblStepName" Content="{Binding StepName, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0"/>
</Grid>
</UserControl>
The code for the Model:
public partial class SingleStepModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyRaised(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
public String StepName
{
get
{
return IrrelevantLogic();
}
}
}
On an other property within this model I call:
OnPropertyRaised(nameof(StepName));
By using the debugger, I can see that the PropertyChanged
is always null and therefore not called. As such, my label is not changed even tough I can see that the StepName
would contain a new value.
I have the suspicion that because I am assigning the DataContext in the ValidationGrid
, the INotifyPropertyChanged
does not get initialized correctly, but I was unable to resolve this issue.
Any help or guidance in the right direction is appreciated.