c# - 将 DataGridTextColumn 的 IsReadOnly 绑定(bind)到 DataGridTemplateColumn 复选框 IsChecked

标签 c# wpf data-binding datagrid

基本上,我有一个包含多个列的 DataGrid,我想启用(更改 IsReadOnly 属性)一个基于CheckBox IsChecked,位于同一个DataGrid的另一个DataGridTemplateColumn中。

这是代码(的重要部分):

<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属性的值,即

  • IsChecked = true => IsReadOnly = false;
  • IsChecked = false => IsReadOnly = true

我可能会用一个简单的 Converter 来实现这一点,但我需要第一部分才能正常工作。

编辑:

回答一个好问题,我的目标是禁用相邻的单元格(同一行),而不是整列。

最佳答案

为您的Scale Column 使用以下绑定(bind):

 <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>

或简单地

<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>

输出:

ReadOnly

PS: Above Solution 1 is specific to your code, cause Auto Scale column is at 0 Index that's why I used Children[0] in Binding. Please change if there is any contextual need.

关于c# - 将 DataGridTextColumn 的 IsReadOnly 绑定(bind)到 DataGridTemplateColumn 复选框 IsChecked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650502/

相关文章:

c# - 从 C# 中的字符串解析 PointF?

c# - 无法将自定义 .css 添加到嵌套的 ASP.NET 表格单元格控件

c# - system.drawing.image 中的“System.OutOfMemoryException”

c# - WPF的 "Stretch.Uniform"等图像FILL逻辑

html - 是否可以给 href ="mailto:"数据绑定(bind) : text? 的值

data-binding - Silverlight 中的清除绑定(bind)(从 SetBinding 中删除数据绑定(bind))

c# - 序列化是否最适合通过套接字发送数据?

.net - WPF Image.Source 缓存过于激进

c# - HttpClient c# - 在 SendASync 处取消了任务

c++ - 在 MVP 中各层移动数据的众所周知的方法是什么