WPF 触发器

发布于 2024-07-14 05:27:57 字数 1085 浏览 7 评论 0原文

我试图设置一个触发器,当我获得的单元格值是某种类型时显示文本块。

我已经成功地在相同的情况下显示了图像,但在这种情况下我不需要图像,而是一些文本。

已注释掉几行以便测试。尝试使其工作。 注释掉的代码有效! 其中的文本块 text=xxx 没有。

这是我的尝试

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

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.

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.

This is my attempts(s)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人哭 2024-07-21 05:27:57

将DataTemplate的内容设置为TextBlock,并根据触发器设置可见性。 我不确定第二个 DataTrigger 的用途,因为它引用当前范围内不存在的目标名称,所以我不知道它如何适合。

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

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>
情释 2024-07-21 05:27:57

杰出的 ! 非常感谢:)

这就是我的最终结果。 如果PrecedenceIndicator =1,则显示!,如果PrecedenceIndicator =2,则显示!!。

<wpfToolkit:DataGridTemplateColumn Header="A" Width="20">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock x:Name="block1" Text="!" Visibility="Collapsed"/>
                <TextBlock x:Name="block2" Text="! !" Visibility="Collapsed"/>
            </TextBlock>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
                    <Setter TargetName="block1" Property="Visibility" Value="Visible"/>
                </DataTrigger>
               <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
                    <Setter TargetName="block2" Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>

Brilliant ! Thanks heaps :)

This is what I ended up with. If PrecedenceIndicator =1, display !, if PrecendenceIndicator =2, display !!.

<wpfToolkit:DataGridTemplateColumn Header="A" Width="20">
    <wpfToolkit:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock x:Name="block1" Text="!" Visibility="Collapsed"/>
                <TextBlock x:Name="block2" Text="! !" Visibility="Collapsed"/>
            </TextBlock>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
                    <Setter TargetName="block1" Property="Visibility" Value="Visible"/>
                </DataTrigger>
               <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
                    <Setter TargetName="block2" Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </wpfToolkit:DataGridTemplateColumn.CellTemplate>
</wpfToolkit:DataGridTemplateColumn>
时光是把杀猪刀 2024-07-21 05:27:57

您需要根据触发器更改 DataTemplate 的类型,从图像更改为文本框。

<DataTemplate>
    <ContentPresenter x:Name="Presenter" />

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
            <Setter TargetName="Presenter" Property="Content">
                <Setter.Value>
                    <TextBlock Text="XXX" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
            <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

You need to change the type of the DataTemplate, based on your trigger, from an Image to a TextBox.

<DataTemplate>
    <ContentPresenter x:Name="Presenter" />

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="1">
            <Setter TargetName="Presenter" Property="Content">
                <Setter.Value>
                    <TextBlock Text="XXX" />
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding PrecedenceIndicator}" Value="2">
            <Setter TargetName="cablePrecedenceIndicatorImage" Property="Source" Value="Resources\Images\small_exclamation_mark.png"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文