禁用WPF中的复选框勾选功能

5
我希望在WPF中(使用C#代码)实现只允许取消选中复选框的功能,而不允许选中。如何做到?
PS: 不接受在单击事件上编写事件处理程序,该事件处理程序会在选中后立即取消选中复选框的方式。
[编辑] 复选框应始终处于启用状态,以便用户认为可以选中复选框。这很奇怪,但这是具体的程序要求。

2
如果用户取消勾选该框,他们是否能够再次勾选它? - Dylan
这个回答解决了你的问题吗?C# WPF中只读CheckBox - Mike Nakis
4个回答

10
<CheckBox Content="Checkbox"
          IsEnabled="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" />

在被告知复选框即使不可选也必须看起来是可选的情况下,进行编辑。以下是一个可行的示例:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfTest.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ControlTemplate x:Key="DeceptiveCheckbox" TargetType="{x:Type CheckBox}">
            <BulletDecorator Background="Transparent" SnapsToDevicePixels="True">
                <BulletDecorator.Bullet>
                    <Microsoft_Windows_Themes:BulletChrome BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" IsChecked="{TemplateBinding IsChecked}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"/>
                </BulletDecorator.Bullet>
                <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </BulletDecorator>
            <ControlTemplate.Triggers>
                <Trigger Property="HasContent" Value="True">
                    <Setter Property="FocusVisualStyle">
                        <Setter.Value>
                            <Style>
                                <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>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Padding" Value="4,0,0,0"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <CheckBox Content="CheckBox"  IsEnabled="{Binding IsChecked, RelativeSource={RelativeSource Self}}" Template="{StaticResource DeceptiveCheckbox}" />
    </Grid>
</Window>

修复很简单。我只需创建当前复选框模板的副本并删除:

<Trigger Property="IsEnabled" Value="False">
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>

3
这个怎么样:
<Grid>
    <Grid.Resources>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <CheckBox IsChecked="True" />
</Grid>

这种方法的好处是可以应用于所有(不作修改)或许多(如果您应用了x:Key属性并将资源分配给所需的复选框),而无需做任何特殊处理或更改各个复选框的绑定。


禁用的复选框的颜色可以更改吗?如果我能够更改复选框矩形的颜色,那么您的解决方案将是很好的。我需要让复选框看起来仍然可以被选中。 - patryk.beza
不行,除非你自己实现控件模板。如果你不想禁用控件,那么你唯一的选择就是编写一些代码后台事件处理程序来决定何时可以或不能选中复选框。 - CodingGorilla

2
一个简单的解决方案是将IsEnabled属性绑定为等于IsChecked属性。

这是一个很好的解决方案,因为它还向用户提供了视觉提示,告诉他们复选框不再可用。(除了应该使用IsEnabled而不是相反的IsReadOnly。IsReadOnly仅适用于TextBoxes)。 - user128300
IsReadOnly 不是 WPF 复选框的有效属性。 - Nick Babcock
复选框应始终处于启用状态,以便用户可以认为可以选中复选框。也许这很奇怪,但它是特定的程序。 - patryk.beza
@patryk.beza 为什么它不能被禁用?你能否通过覆盖禁用控件的UI效果来欺骗用户认为它已启用? - Kevin DiTraglia

0
要使禁用复选框的可见性与启用的复选框相似,并具有禁用属性,请在触发器中将不透明度设置为0。
<CheckBox.Resources>
     <Style TargetType="CheckBox">
           <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                      <Setter Property="Opacity" Value="1"/>
                </Trigger>
            </Style.Triggers>
       </Style>
</CheckBox.Resources>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接