如何删除(3 个状态)选中的列表框项目周围不需要的红色边框?
How to remove unwanted red border around (3 state) checked listbox items?
我创建了一个 ListBox,每个 ListBox 项都有一个三态 CheckBox,每个状态都有一个自定义 CheckBox 图像。问题是,当项目设置为第三种 (null
) 状态时,项目周围会出现不需要的红色边框,我不知道为什么会出现这种情况或如何删除它。
列表框的样式如下:
<Style x:Key="ThreeStateCheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="Margin" Value="8"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False" Foreground="{StaticResource textBrush}"
IsThreeState="True" IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Image x:Name="checkboxImage" Source="pack://application:,,,/Images/CheckBoxUncheck_16x.png" Width="16"
Margin="0,0,2,0" />
<ContentPresenter/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="checkboxImage" Property="Source" Value="pack://application:,,,/Images/CheckBox_16x.png"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="checkboxImage" Property="Source" Value="pack://application:,,,/Images/CheckBoxExclude_16x.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
在我看来像这样的绑定
IsChecked="{Binding Path=IsSelected,
Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}"
负责观测结果,因为CheckBox.IsChecked
可以是True
| False
| null
(Nullable<bool>
), 当 ListBoxItem.IsSelected
为 bool
如果我设置详细的跟踪信息,使用 PresentationTraceSources.TraceLevel=High
IsChecked="{Binding Path=IsSelected,
Mode=TwoWay,
PresentationTraceSources.TraceLevel=High,
RelativeSource={RelativeSource TemplatedParent}}"
我得到:
BindingExpression : GetValue at level 0 from ListBoxItem using DependencyProperty(IsSelected): 'False'
BindingExpression : TransferValue - got raw value 'False'
BindingExpression : TransferValue - using final value 'False'
BindingExpression : Update - got raw value 'True'
BindingExpression : Update - using final value 'True'
BindingExpression : SetValue at level 0 to ListBoxItem using DependencyProperty(IsSelected): 'True'
BindingExpression : GetValue at level 0 from ListBoxItem using DependencyProperty(IsSelected): 'True'
BindingExpression : TransferValue - got raw value 'True'
BindingExpression : TransferValue - using final value 'True'
BindingExpression : Update - got raw value <null>
BindingExpression : Update - using final value {DependencyProperty.UnsetValue}
null
导致 {DependencyProperty.UnsetValue} 并且它是 bool IsSelected 属性
的错误
项目周围的红色边框是默认设置 Validation.ErrorTemplate
。您可以在 CheckBox
上禁用它
<CheckBox Validation.ErrorTemplate="{x:Null}" Focusable="False" ...
请注意,处于不确定状态 (IsChecked=null) 的 CheckBox 不会清除对 ListBoxItem 的选择
我创建了一个 ListBox,每个 ListBox 项都有一个三态 CheckBox,每个状态都有一个自定义 CheckBox 图像。问题是,当项目设置为第三种 (null
) 状态时,项目周围会出现不需要的红色边框,我不知道为什么会出现这种情况或如何删除它。
列表框的样式如下:
<Style x:Key="ThreeStateCheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="Margin" Value="8"></Setter>
<Setter Property="Background" Value="Transparent"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False" Foreground="{StaticResource textBrush}"
IsThreeState="True" IsChecked="{Binding Path=IsSelected, Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}">
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Image x:Name="checkboxImage" Source="pack://application:,,,/Images/CheckBoxUncheck_16x.png" Width="16"
Margin="0,0,2,0" />
<ContentPresenter/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="checkboxImage" Property="Source" Value="pack://application:,,,/Images/CheckBox_16x.png"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="checkboxImage" Property="Source" Value="pack://application:,,,/Images/CheckBoxExclude_16x.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
<ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
在我看来像这样的绑定
IsChecked="{Binding Path=IsSelected,
Mode=TwoWay,
RelativeSource={RelativeSource TemplatedParent}}"
负责观测结果,因为CheckBox.IsChecked
可以是True
| False
| null
(Nullable<bool>
), 当 ListBoxItem.IsSelected
为 bool
如果我设置详细的跟踪信息,使用 PresentationTraceSources.TraceLevel=High
IsChecked="{Binding Path=IsSelected,
Mode=TwoWay,
PresentationTraceSources.TraceLevel=High,
RelativeSource={RelativeSource TemplatedParent}}"
我得到:
BindingExpression : GetValue at level 0 from ListBoxItem using DependencyProperty(IsSelected): 'False'
BindingExpression : TransferValue - got raw value 'False'
BindingExpression : TransferValue - using final value 'False'
BindingExpression : Update - got raw value 'True'
BindingExpression : Update - using final value 'True'
BindingExpression : SetValue at level 0 to ListBoxItem using DependencyProperty(IsSelected): 'True'
BindingExpression : GetValue at level 0 from ListBoxItem using DependencyProperty(IsSelected): 'True'
BindingExpression : TransferValue - got raw value 'True'
BindingExpression : TransferValue - using final value 'True'
BindingExpression : Update - got raw value <null>
BindingExpression : Update - using final value {DependencyProperty.UnsetValue}
null
导致 {DependencyProperty.UnsetValue} 并且它是 bool IsSelected 属性
项目周围的红色边框是默认设置 Validation.ErrorTemplate
。您可以在 CheckBox
<CheckBox Validation.ErrorTemplate="{x:Null}" Focusable="False" ...
请注意,处于不确定状态 (IsChecked=null) 的 CheckBox 不会清除对 ListBoxItem 的选择