I am trying to get my DataTrigger on my image to fire when a boolean is changed in my ViewModel. The code for the image control is:
<Image>
<Image.Style>
<Style TargetType="Image">
<Setter Property="Width" Value="15"/>
<Setter Property="Height" Value="15"/>
<Setter Property="Source" Value="{Binding CloseImage}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding TASI}" Value="True">
<Setter Property="Source" Value="{Binding CheckImage}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
The ViewModel code is:
private bool _TASI;
public bool TASI
{
get { return _TASI; }
set
{
_TASI = value;
SetPropertyChanged("_TASI");
}
}
However it seems that when the bool is changed from false to true, the image does not change. The value is changed via an async method:
public async void StartUpRoutine()
{
await Task.Run(() => PingServer());
MessageBox.Show("Server Pinged");
}
public void PingServer()
{
if (Directory.Exists(ServerAddress))
{
TASI = true;
MessageBox.Show("Server Found");
}
else
TASI = false;
}
I know the directory exists because the MessageBox is popping up showing "Server Found". But the DataTrigger isn't firing in my XAML. I can see the CloseImage but not the CheckImage. If I change it to false:
<DataTrigger Binding="{Binding TASI}" Value="False">
Then I see the CheckImage and not the CloseImage. So the Image Sources are set right, they just aren't changing when the value is changed in the ViewModel.
You are raising the PropertyChanged event for the wrong property name.
SetPropertyChanged("_TASI");
needs to be
SetPropertyChanged("TASI");
or better
SetPropertyChanged(nameof(TASI));