禁用WPF中的复选框检查
我想在WPF中设置不可能的复选框(来自C#代码)。只允许取消勾选。禁用WPF中的复选框检查,wpf,checkbox,checkboxpreference,Wpf,Checkbox,Checkboxpreference,我想在WPF中设置不可能的复选框(来自C#代码)。只允许取消勾选。 我该怎么做 PS在单击事件时编写事件处理程序,在选中该事件后立即取消选中复选框是不可接受的 [编辑]复选框应始终处于启用状态,以便用户认为可以选中该复选框。这很奇怪,但它是一个特定的程序。一个简单的解决方案是将IsEnabled属性绑定为等于IsChecked属性。 <CheckBox Content="Checkbox" IsEnabled="{Binding RelativeSource={Rela
我该怎么做 PS
在单击事件时编写事件处理程序,在选中该事件后立即取消选中复选框是不可接受的
[编辑]
复选框应始终处于启用状态,以便用户认为可以选中该复选框。这很奇怪,但它是一个特定的程序。一个简单的解决方案是将IsEnabled属性绑定为等于IsChecked属性。
<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>
这个怎么样:
<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
属性,然后将资源分配给所需的复选框)无需执行任何特殊操作或更改各个复选框的绑定。如果用户取消选中该复选框,他们是否能够再次选中该复选框?这是否回答了您的问题?这是一个很好的解决方案,因为它还向用户提供了一个视觉提示,即复选框不再可以使用。(除了应该是IsEnabled,而不是相反。IsReadOnly仅用于文本框)。IsReadOnly不是wpf checkboxCheckbox的有效属性应始终启用,以便用户认为可以选中该复选框。也许这很奇怪,但它是特定的程序。@patryk.beza为什么不能禁用它?您是否可以通过覆盖禁用控件的UI效果来诱使用户认为它已启用?是否可以更改禁用复选框的颜色?如果我可以更改复选框矩形的颜色,您的解决方案会很好。我需要让复选框看起来仍然是“可检查的”。不,没有实现您自己的控制模板。如果您不想禁用该控件,那么您唯一的选择就是使用一些代码隐藏事件处理程序来决定何时可以选中或不可以选中复选框。