WPF 触发器 [英] WPF Triggers

查看:29
本文介绍了WPF 触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我为单元格获得的值是特定类型时,我正在尝试设置触发器以显示文本块.

I'm trying to set a trigger to display a block of text when the value i get for the cell is a certain type.

我已经成功地在同样的情况下显示了一张图片,但在这种情况下我不想要一张图片,而是一些文字.

I have successfully managed to display an image in the same situation, but in this circumstance i don't want an image, but some text.

已注释掉几行以进行测试.尝试使其工作.注释掉的代码有效!里面的 textblock text=xxx 没有.

Have commented out lines in order to test.try to make it work. The commented out code works ! The textblock text=xxx inside it, doesn't.

这是我的尝试

<wpfToolkit:DataGridTemplateColumn Header="P" Width="20">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <!-- <DataTemplate.Triggers> -->
            <!-- <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1"> -->
            <TextBlock Text="XXX" />
            <!-- </DataTrigger> -->
            <!-- <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2"> -->
            <!-- <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/> -->
            <!-- </DataTrigger> -->
            <!-- </DataTemplate.Triggers> -->
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>

推荐答案

将 DataTemplate 的内容设置为 TextBlock 并根据触发器设置可见性.我不确定第二个 DataTrigger 是干什么用的,因为它指的是当前作用域中不存在的目标名称,所以我不知道它是如何适应的.

Set the content of the DataTemplate to the TextBlock and set the visibility based on the trigger. I'm not sure what the second DataTrigger is for because it is referring to a target name that does not exist in the current scope, so I don't know how this fits in.

<DataTemplate>
    <TextBlock x:Name="block" Text="XXX" Visibility="Collapsed"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
            <Setter TargetName="block" Property="Visibility" Value="Visible"/>
        </DataTrigger>
        <!--<DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
            <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/>
        </DataTrigger>-->
    </DataTemplate.Triggers>
</DataTemplate>

这篇关于WPF 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆