在 WPF 中使用图片或图标代替 DataGridCheckBoxColumn

2023-12-14

我想在选中时将 DataGridColumn 内部的复选框更改为图像,在未选中时将其更改为图像,我该怎么办? Ps:我的DataGridCheckBoxColumn定义如下:

 <DataGridCheckBoxColumn Header="Priority" Binding="{Binding PRIORITY, Converter={StaticResource converter}}"/>

转换器正在将字节转换为布尔值。


Use the ElementStyle and EditingElementStyle属性来创建和设置不同的Template为了CheckBox这很适合。

e.g.

<DataGridCheckBoxColumn Binding="{Binding IsActive}">
    <DataGridCheckBoxColumn.ElementStyle>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <Image MaxWidth="32" MaxHeight="32">
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Setter Property="Source" Value="Images/Error.ico" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType=CheckBox}}" Value="True">
                                            <Setter Property="Source" Value="Images/Default.ico" />
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>

这使得该列显示基于的图像IsChecked,URI 只是硬编码,并且 CheckBox 被禁用,因为在 ElementStyle 中进行编辑不会更改绑定对象上的任何属性。它的唯一目的是显示适当的图像。

(The EditingElementStyle此处未设置,因此如果用户再次单击单元格进行编辑,则会出现一个可以选中或取消选中的普通复选框。)

Screencap

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WPF 中使用图片或图标代替 DataGridCheckBoxColumn 的相关文章

随机推荐