问题描述
基本上,我有一个带有几列的 DataGrid
,并且我想启用(更改 IsReadOnly
属性)基于 CheckBox
IsChecked
的 DataGridTextColumn
,位于另一个 DataGridTemplateColumn
与相同的 DataGrid
。
Basically, I have a DataGrid
with several columns, and I want to enable (changing the IsReadOnly
property) a DataGridTextColumn
based on a CheckBox
IsChecked
, located in another DataGridTemplateColumn
of the same DataGrid
.
这里是(代码的重要部分:
Here is (the important part of) the code:
<DataGrid Name="lstTags" Grid.Row="0" ItemsSource="{Binding Path = LinesCollection}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" SelectionMode="Single" LostFocus="lstTags_LostFocus" SelectionChanged="lstTags_SelectionChanged">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="colAutoScale" Header="Auto Scale">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="ckbAutoScale" HorizontalAlignment="Center" IsChecked="{Binding AutoScale, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Scale" Binding="{Binding Path=Scale}" IsReadOnly="{Binding ElementName ckbAutoScale, Path=IsChecked}" Width="60" />
</DataGrid.Columns>
</DataGrid>
值得一提的是,我还想反转IsChecked属性的值,即
It is worth mentioning that I also want to invert the value of the IsChecked property, that is
-
IsChecked = true
=>IsReadOnly = false
; -
IsChecked = false
=>IsReadOnly = true
IsChecked = true
=>IsReadOnly = false
;IsChecked = false
=>IsReadOnly = true
.
我可能会用一个简单的 Converter
来实现这一点,但是我需要
I would probably achieve this with a simple Converter
, but I need that first part working tho.
编辑:
回答一个好问题,我的目标是禁用相邻单元格(同一行),而不是整个列。
Answering a good question, my goal is to disable the adjacent cell (same row), not the whole column.
推荐答案
对您的使用以下绑定> 规模
列
:
<DataGridTextColumn Header="Scale" Binding="{Binding Path=Scale}" Width="60" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridCellsPanel}},Path=Children[0].Content.Content.AutoScale}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
或简单地
OR simply
<DataGridTextColumn Header="Scale" Binding="{Binding Path=Scale}" Width="60" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding Path=AutoScale}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
输出:
这篇关于将DataGridTextColumn的IsReadOnly绑定到DataGridTemplateColumn复选框IsChecked的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!