DataTrigger Usage

后端 未结 2 1220
傲寒
傲寒 2021-01-25 01:55

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 == {         


        
相关标签:
2条回答
  • 2021-01-25 02:32

    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 DataTriggers 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:

    1. Set the source to AvatarPath
    2. If AvatarPath is null, set the source to 'Img_Male'
    3. If the AvatarPath is null AND the Gender is female, set the source to 'Img_Female'
    0 讨论(0)
  • 2021-01-25 02:43

    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.

    0 讨论(0)
提交回复
热议问题