gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:12:37 31 4
gpt4 key购买 nike

基本上,我有一个包含多个列的 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com