DataTrigger的用法 [英] DataTrigger Usage
问题描述
这里是我要在xaml中实现的伪代码
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 == {x:Static vm:Gender.Female} THEN
Image.Source = {StaticResource Img_Female}
ELSE
Image.Source = {StaticResource Img_Male}
ENDIF
ENDIF
及以下是至少有以下问题的实现尝试:
and below is an implementation attempt with at least the following issues:
- 它如何知道AvatarPath为空并且我们关心关于性别?
- 有没有一种方法可以做ELSE,所以我只能指定Gender.Male资源一次,而不是
一次
如何正确实施?
How can I implement this properly?
干杯,
Berryl
Cheers,
Berryl
<DataTemplate x:Key="AvatarPathTemplate">
<Image x:Name="avatarImage" Source="{Binding AvatarPath}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Gender}" Value="{x:Static vm:Gender.Female}">
<Setter Property="Sourrce" Value="{resx:Resx Img_Female}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Gender}" Value="{x:Static vm:Gender.Male}">
<Setter Property="Sourrce" Value="{resx:Resx Img_Male}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Gender}" Value="{x:Static vm:Gender.Unknown}">
<Setter Property="Sourrce" Value="{resx:Resx Img_Male}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Gender}" Value="{x:Static vm:Gender.Unspecified}">
<Setter Property="Sourrce" Value="{resx:Resx Img_Male}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
更新
正如trimeyko指出的那样,
update
As trimeyko points out, this could be done either with a multiconverter or inside of a view model.
根据我的回答:我实际上在以下位置尝试了多转换器方法:首先,取得了一定的成功,并且几乎将其发布以帮助清理它。然后,我决定最好让转换器来实际转换类型。同意视图模型方法可能是最简单的,但这似乎更像是视图的工作,我我想看看是否可以先使它像这样工作。
Per my answer back: "I actually tried the multiconverter approach at first, with modest success, and almost posted that to help clean it up. Then I decided that converters are best left to actually converting types. Agreed the view model approach is probably easiest but this does seem more to be the view's job, and I'd like to see if I can get it to work as such first."
我尝试了[通过在此处发布mutliConveter解决此问题]( MultiConverter用法)
I made my attempt at [solving this with a mutliConveter posting here] (MultiConverter usage)
推荐答案
您应该可以使用几个 MultiDataTrigger
s:
You should be able to do this with a couple of MultiDataTrigger
s:
<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>
这些表示当 AvatarPath
为空时AND 性别
是女性...'
These are stating 'when AvatarPath
is null AND Gender
is female...'
进一步的改进
由于 DataTrigger
按照XAML中出现的顺序应用,因此我们无需重复输入男性 '在以下示例中进行设置:
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>
在这里我们说的是:
- 将源设置为
AvatarPath
- 如果
AvatarPath
为空,则将源设置为'Img_Male' - 如果
AvatarPath
为空且性别
是女性,将来源设置为 Img_Female
- Set the source to
AvatarPath
- If
AvatarPath
is null, set the source to 'Img_Male' - If the
AvatarPath
is null AND theGender
is female, set the source to 'Img_Female'
这篇关于DataTrigger的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!