WPF 复选框自定义样式检查消失

Posted

技术标签:

【中文标题】WPF 复选框自定义样式检查消失【英文标题】:WPF Checkbox Custom Style Check disappeared 【发布时间】:2021-05-20 21:06:49 【问题描述】:

也许这是多余的,但我找不到任何东西。我可能错过了一些简单的东西。

有问题的复选框是在选择事件的代码隐藏中设置的

  chkEmpOK.IsChecked = _cfgUsr.IsEnabled;

在我尝试添加自定义样式之前,这可以正常工作。现在复选框显示文本,但复选框的图像/字形/框部分不显示。我假设我在风格上做错了什么。这是来自 XAML 的控件和样式:

<CheckBox x:Name="chkEmpOK" Grid.Column="2" Grid.Row="4" Margin="10,10,0,0">
    <CheckBox.Style>
        <Style TargetType="x:Type CheckBox">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="x:Type CheckBox">
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter Property="Content" Value="Click Me to Enable"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Content" Value="To remove access- click me"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                        <ContentPresenter Content="TemplateBinding Content"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </CheckBox.Style>
</CheckBox>

这就是它的样子

希望我错过了一些简单的东西。跟继承ToggleButton有关系吗?

【问题讨论】:

【参考方案1】:

无需重写模板来更改内容或前景。样式触发器应该这样做:

<CheckBox x:Name="chkEmpOK" Grid.Column="2" Grid.Row="4" Margin="10,10,0,0">
    <CheckBox.Style>
        <Style TargetType="x:Type CheckBox">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="Content" Value="Click Me to Enable"/>
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Content" Value="To remove access- click me"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

【讨论】:

您是绝对正确的,我喜欢您的简单回答。在另一个应用程序中,我为 IsChecked(true 和 false)和 IsEnabled(False)设置了触发器,设置 Content、Foreground、FontWeight 和 Style,用户对可用性非常满意。【参考方案2】:

您正在用自己的没有与框相关的代码覆盖原始复选框样式。导航到 Document Outline 窗口,右键单击您的复选框,然后单击 Edit Template -> Edit Copy。

然后你可以将模板样式导入到窗口资源并进行编辑:

<Window.Resources>
    <Style x:Key="FocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="DynamicResource x:Static SystemColors.ControlTextBrushKey" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="OptionMark.Static.Background" Color="#FFFFFFFF"/>
    <SolidColorBrush x:Key="OptionMark.Static.Border" Color="#FF707070"/>
    <Style x:Key="OptionMarkFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="14,0,0,0" SnapsToDevicePixels="true" Stroke="DynamicResource x:Static SystemColors.ControlTextBrushKey" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Background" Color="#FFF3F9FF"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Border" Color="#FF5593FF"/>
    <SolidColorBrush x:Key="OptionMark.MouseOver.Glyph" Color="#FF212121"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Background" Color="#FFE6E6E6"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Border" Color="#FFBCBCBC"/>
    <SolidColorBrush x:Key="OptionMark.Disabled.Glyph" Color="#FF707070"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Background" Color="#FFD9ECFF"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Border" Color="#FF3C77DD"/>
    <SolidColorBrush x:Key="OptionMark.Pressed.Glyph" Color="#FF212121"/>
    <SolidColorBrush x:Key="OptionMark.Static.Glyph" Color="#FF212121"/>
    <Style x:Key="CheckBoxStyle1" TargetType="x:Type CheckBox">
        <Setter Property="FocusVisualStyle" Value="StaticResource FocusVisual"/>
        <Setter Property="Background" Value="StaticResource OptionMark.Static.Background"/>
        <Setter Property="BorderBrush" Value="StaticResource OptionMark.Static.Border"/>
        <Setter Property="Foreground" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="x:Type CheckBox">
                    <Grid x:Name="templateRoot" Background="Transparent" SnapsToDevicePixels="True">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border x:Name="checkBoxBorder" BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Background="TemplateBinding Background" HorizontalAlignment="TemplateBinding HorizontalContentAlignment" Margin="1" VerticalAlignment="TemplateBinding VerticalContentAlignment">
                            <Grid x:Name="markGrid">
                                <Path x:Name="optionMark" Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z " Fill="StaticResource OptionMark.Static.Glyph" Margin="1" Opacity="0" Stretch="None"/>
                                <Rectangle x:Name="indeterminateMark" Fill="StaticResource OptionMark.Static.Glyph" Margin="2" Opacity="0"/>
                            </Grid>
                        </Border>
                        <ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False" HorizontalAlignment="TemplateBinding HorizontalContentAlignment" Margin="TemplateBinding Padding" RecognizesAccessKey="True" SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" VerticalAlignment="TemplateBinding VerticalContentAlignment"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasContent" Value="true">
                            <Setter Property="FocusVisualStyle" Value="StaticResource OptionMarkFocusVisual"/>
                            <Setter Property="Padding" Value="4,-1,0,0"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" TargetName="checkBoxBorder" Value="StaticResource OptionMark.MouseOver.Background"/>
                            <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="StaticResource OptionMark.MouseOver.Border"/>
                            <Setter Property="Fill" TargetName="optionMark" Value="StaticResource OptionMark.MouseOver.Glyph"/>
                            <Setter Property="Fill" TargetName="indeterminateMark" Value="StaticResource OptionMark.MouseOver.Glyph"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="checkBoxBorder" Value="StaticResource OptionMark.Disabled.Background"/>
                            <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="StaticResource OptionMark.Disabled.Border"/>
                            <Setter Property="Fill" TargetName="optionMark" Value="StaticResource OptionMark.Disabled.Glyph"/>
                            <Setter Property="Fill" TargetName="indeterminateMark" Value="StaticResource OptionMark.Disabled.Glyph"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" TargetName="checkBoxBorder" Value="StaticResource OptionMark.Pressed.Background"/>
                            <Setter Property="BorderBrush" TargetName="checkBoxBorder" Value="StaticResource OptionMark.Pressed.Border"/>
                            <Setter Property="Fill" TargetName="optionMark" Value="StaticResource OptionMark.Pressed.Glyph"/>
                            <Setter Property="Fill" TargetName="indeterminateMark" Value="StaticResource OptionMark.Pressed.Glyph"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Opacity" TargetName="optionMark" Value="1"/>
                            <Setter Property="Opacity" TargetName="indeterminateMark" Value="0"/>
                            <Setter Property="Content" Value="To remove access- click me" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter Property="Content" Value="Click Me to Enable" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="x:Null">
                            <Setter Property="Opacity" TargetName="optionMark" Value="0"/>
                            <Setter Property="Opacity" TargetName="indeterminateMark" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

根据需要编辑相关触发器:

<Trigger Property="IsChecked" Value="true">
    <Setter Property="Opacity" TargetName="optionMark" Value="1"/>
    <Setter Property="Opacity" TargetName="indeterminateMark" Value="0"/>
    <Setter Property="Content" Value="To remove access- click me" />
</Trigger>
<Trigger Property="IsChecked" Value="false">
    <Setter Property="Content" Value="Click Me to Enable" />
</Trigger>

另一种方法是为盒子添加自己的代码。 就像带有边框和矢量图像的 StackPanel。请参见下面的示例:

<CheckBox x:Name="chkEmpOK" Margin="10,10,0,0">
        <CheckBox.Style>
            <Style TargetType="x:Type CheckBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="x:Type CheckBox">
                            <StackPanel Orientation="Horizontal"
                                        SnapsToDevicePixels="True">
                                <Border Width="15"
                                        Height="14"
                                        BorderThickness="1"
                                        BorderBrush="DimGray"
                                        VerticalAlignment="Center"
                                        Background="TemplateBinding Background">
                                    <Viewbox Margin="1">
                                        <Path
                                            x:Name="ckeckMark"
                                            Fill="Black"
                                            Data="F1 M 9.97498,1.22334L 4.6983,9.09834L 4.52164,9.09834L 0,5.19331L 1.27664,3.52165L 4.255,6.08833L 8.33331,1.52588e-005L 9.97498,1.22334 Z " />
                                    </Viewbox>
                                </Border>
                                <ContentPresenter Margin="5 0 0 0" 
                                                  Focusable="False"/>
                            </StackPanel>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="False">
                                    <Setter Property="Content" Value="Click Me to Enable" />
                                    <Setter Property="Foreground" Value="Red" />
                                    <Setter Property="Visibility" Value="Hidden" TargetName="ckeckMark" />
                                </Trigger>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Content" Value="To remove access- click me" />
                                    <Setter Property="Visibility" Value="Visible" TargetName="ckeckMark" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </CheckBox.Style>
    </CheckBox>

另一种方法是使用 Checked 和 Unchecked 事件来设置复选框的内容和颜色。如果您的程序很大,请考虑使用 MVVM 模式。

【讨论】:

首先,谢谢。通过添加您自己的代码,我假设您指的是控件模板是 Grid 或 StackPanel 的其他示例,然后在控件模板中放置一个选中或取消选中的图像和文本。 我用自定义框样式的示例扩展了答案。您可以使用任何您认为最适合您的情况的面板。

以上是关于WPF 复选框自定义样式检查消失的主要内容,如果未能解决你的问题,请参考以下文章

WPF:ListView自定义scrollviewer导致列标题消失

在 wpf 中的自定义样式上,文本框的文本始终为空

WPF自定义控件与样式-ScrollViewer与ListBox自定义样式

WPF自定义控件与样式-列表控件DataGrid与ListView自定义样式

WPF自定义控件与样式(15)-终结篇 & 系列文章索引 & 源码共享

WPF自定义控件与样式(10)-进度控件ProcessBar自定义样