Here is pseudo code for what I want to implement in xaml
IF vm.AvatarFilePath IS NOT NULL THEN
Image.Source = {Binding AvatarPath}
ELSE
If vm.Gender == {
You should be able to do this with a couple of MultiDataTriggers:
<DataTemplate x:Key="AvatarPathTemplate">
<Image x:Name="avatarImage" Source="{Binding AvatarPath}"/>
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
<Condition Binding="{Binding Gender}" Value="{x:Static vm:Gender.Female}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{resx:Resx Img_Female}"/>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
<Condition Binding="{Binding Gender}" Value="{x:Static vm:Gender.Male}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{resx:Resx Img_Male}"/>
</MultiDataTrigger>
<!-- etc... -->
</DataTemplate.Triggers>
</DataTemplate>
These are stating 'when AvatarPath
is null AND Gender
is female...'
Further Improvement
As DataTrigger
s are applied in the order in which they appear in the XAML, we can remove the need for duplication of the 'Male' settings in the example with the below:
<DataTemplate x:Key="AvatarPathTemplate">
<Image x:Name="avatarImage" Source="{Binding AvatarPath}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding AvatarPath}" Value="{x:Null}">
<Setter Property="Source" Value="{resx:Resx Img_Male}"/>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding AvatarPath}" Value="{x:Null}" />
<Condition Binding="{Binding Gender}" Value="{x:Static vm:Gender.Female}"/>
</MultiDataTrigger.Conditions>
<Setter Property="Source" Value="{resx:Resx Img_Female}"/>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Here we are saying:
AvatarPath
AvatarPath
is null, set the source to 'Img_Male'AvatarPath
is null AND the Gender
is female, set the source to 'Img_Female'As an option you can use custom converter class and convert viewmodel to bitmap source. If you wish to use triggers, then you can use some multidatatriggers and/or multiconverters for example for cases when you want to show Img_Male.
But these solutions isn't really good I think, better to introduce property/logic and simply bind image source to it and handle these view logic inside viewmodel. Using this approach you can write unit tests for this logic also.