在WPF中使用Picture或Icon代替DataGridCheckBoxColumn [英] Using Picture or Icon instead of DataGridCheckBoxColumn in WPF

查看:138
本文介绍了在WPF中使用Picture或Icon代替DataGridCheckBoxColumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将CheckBox在DataGridColumn内部更改为一个图像,当它被检查时,另一个在未被选中时,我该怎么办?
Ps:我的DataGridCheckBoxColumn定义如下:

I want to change the CheckBox which is inside of a DataGridColumn into an image when it is checked and another when it is unchecked, How can i do ? Ps: My DataGridCheckBoxColumn is defined like this:

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

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

Converter is converting bytes to boolean.

推荐答案

使用 ElementStyle EditingElementStyle 属性来创建和设置不同的 模板 CheckBox 适合于此。

例如

<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只是硬编码,复选框被禁用,因为在ElementStyle中的编辑不会更改绑定对象上的任何属性。

This makes the column display an image based on IsChecked, the URIs are just hardcoded and the CheckBox is disabled because editing in the ElementStyle does not change any properties on the bound object. Its only purpose is to display the approriate image.

EditingElementStyle 未设置在此处,因此如果用户再次单击该单元格进行编辑,可以检查或取消选中正常的CheckBox。

(The EditingElementStyle is not set here so if the user clicks the cell again to edit it a normal CheckBox appears which can be checked or unchecked.)

这篇关于在WPF中使用Picture或Icon代替DataGridCheckBoxColumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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