我正在使用 ObservableCollection 的 ICollectionView 在 DataGrid 中显示数据。每个条目都包含一个 IsEnabled 属性(显示在 DataGridCheckBoxColumn 中)和两个整数值(每个都在一个 DataGridTextColumn 中)。当 IsEnabled 为 false 时,使用此样式禁用整行:
<Style TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
数据网格如下所示:
<DataGrid
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="false"
AutoGenerateColumns="false"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=ListView, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Name" Width="*" IsReadOnly="True"
Binding="{Binding Path=Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Left"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="*" Binding="{Binding Path=Min, Mode=TwoWay}">
<DataGridTextColumn.Header>
<TextBlock HorizontalAlignment="center" Text="Min"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Width="*" Binding="{Binding Path=Max, Mode=TwoWay}">
<DataGridTextColumn.Header>
<TextBlock HorizontalAlignment="center" Text="Max"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
我遇到了这种奇怪的行为:例如,我在数据网格中有两行。第 1 行启用(该项的 IsEnabled 为 true),第 2 行禁用(该项的 IsEnabled 为 false)。如果我编辑第 1 行中的一个整数值并按 ENTER,则绑定将按预期更新。当我用鼠标指针再次单击同一个文本框时,禁用行 (2) 下方的文本块被激活(并以某种灰色突出显示)。如果我想编辑第 1 行中的文本块,我必须再次单击其他地方才能离开第 2 行中的“聚焦”文本块,并且通过第三次单击,我可以回到最初编辑的文本块。
这是数据网格中的错误吗?我该如何纠正这个问题,以便在再次单击第 1 行后再次编辑文本框?
有任何想法吗?
谢谢!