我想要在选中 DataGridColumn 中的 CheckBox 时将其更改为一张图片,而在未选中时则是另一张图片。我该怎么做呢? Ps:我的 DataGridCheckBoxColumn 定义如下:
<DataGridCheckBoxColumn Header="Priority" Binding="{Binding PRIORITY, Converter={StaticResource converter}}"/>
转换器正在将字节转换为布尔值。
我想要在选中 DataGridColumn 中的 CheckBox 时将其更改为一张图片,而在未选中时则是另一张图片。我该怎么做呢? Ps:我的 DataGridCheckBoxColumn 定义如下:
<DataGridCheckBoxColumn Header="Priority" Binding="{Binding PRIORITY, Converter={StaticResource converter}}"/>
转换器正在将字节转换为布尔值。
ElementStyle
和 EditingElementStyle
属性来创建并设置不同的 Template
,以适配 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被硬编码,CheckBox被禁用,因为在ElementStyle中编辑不会改变绑定对象的任何属性。它的唯一目的是显示适当的图片。
(这里没有设置EditingElementStyle
,所以如果用户再次点击单元格进行编辑,则会出现一个普通的CheckBox,可以选中或取消选中。)
ElementStyle
应该可以正常工作,你是否设置了EditingElementStyle
? - H.B.