如何在 WPF/MVVM 中将 ControlTemplate(用于复选框)设置为只读?
我有一个 WPF 数据网格,在其中为复选框创建了一个 ControlTemplate 来表示 bool?
类型。 我希望复选框/控件模板对用户只读,但能够更改值
这是模板:
<ControlTemplate x:Key="checkboxTemplate" TargetType="CheckBox">
<Grid Background="{TemplateBinding Background}" Height="{TemplateBinding Height}">
<Rectangle x:Name="r" Height="10" Width="40" HorizontalAlignment="Center" VerticalAlignment="Center" RadiusX="4" RadiusY="4"></Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="CheckBox.IsChecked" Value="True">
<Setter Property="Fill" Value="#FF66D660" TargetName="r"></Setter>
</Trigger>
<Trigger Property="CheckBox.IsChecked" Value="False">
<Setter Property="Fill" Value="#FFD50005" TargetName="r"></Setter>
</Trigger>
<Trigger Property="CheckBox.IsChecked" Value="{x:Null}">
<Setter Property="Fill" Value="SlateGray" TargetName="r"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这是数据网格中的实现:
<DataGridTemplateColumn SortMemberPath="IsReady" Header="Ready" CanUserSort="True" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsReady, Mode=OneWay}" HorizontalAlignment="Center" Template="{StaticResource checkboxTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I've got a WPF datagrid where I've created a ControlTemplate for a checkbox to represent a bool?
type.
I'd like for the checkbox/control template to readonly to the user, but be able to change the value
Here's the template:
<ControlTemplate x:Key="checkboxTemplate" TargetType="CheckBox">
<Grid Background="{TemplateBinding Background}" Height="{TemplateBinding Height}">
<Rectangle x:Name="r" Height="10" Width="40" HorizontalAlignment="Center" VerticalAlignment="Center" RadiusX="4" RadiusY="4"></Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="CheckBox.IsChecked" Value="True">
<Setter Property="Fill" Value="#FF66D660" TargetName="r"></Setter>
</Trigger>
<Trigger Property="CheckBox.IsChecked" Value="False">
<Setter Property="Fill" Value="#FFD50005" TargetName="r"></Setter>
</Trigger>
<Trigger Property="CheckBox.IsChecked" Value="{x:Null}">
<Setter Property="Fill" Value="SlateGray" TargetName="r"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Here's the implementation in the datagrid:
<DataGridTemplateColumn SortMemberPath="IsReady" Header="Ready" CanUserSort="True" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsReady, Mode=OneWay}" HorizontalAlignment="Center" Template="{StaticResource checkboxTemplate}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在复选框上设置 IsHitTestVisible="false" 或 IsEnabled="false"。
You can either set IsHitTestVisible="false" or IsEnabled="false" on the checkbox.
使其只读的最简单方法是将其移动到具有几乎透明背景的某些控件后面。
Easiest way to make it readonly to move it behind some control with almost transparent background.
如果您要通过代码编辑 Checked 值,则应该编辑绑定源 (
IsReady
),而不是CheckBox.Checked
值。设置MyCheckBox.Checked
将覆盖绑定,并且不会将更改保存到IsReady
属性。最好从 ViewModel 更改
IsReady
属性,但如果必须从 View 后面执行此操作,我通常将 CheckBox 的 DataContext 转换为我的数据对象,并从那里设置绑定属性,如下所示:
至于使 CheckBox 对用户只读,请将其
IsEnabled
属性设置为 falseIf you're editing the Checked value from code, you should be editing the binding source (
IsReady
), and not theCheckBox.Checked
value. SettingMyCheckBox.Checked
will overwrite the binding, and not save the change to yourIsReady
property.It's preferred to change the
IsReady
property from your ViewModel, but if you must do it from behind the View I usually cast the CheckBox's DataContext to my data object, and set the bound property from thereSomething like this:
As for making a CheckBox read-only to the user, set it's
IsEnabled
property to false