如何限制在WPF中选择特定的datagrid行?

时间:2017-09-12 10:44:45

标签: c# wpf datagrid

我有一个DataGrid,我在其中使用DataGrids LoadingRow事件以红色标记重复记录。

Xaml:

<DataGrid x:Name="dataGrid" 
                  EnableRowVirtualization="False" 
                  AutoGenerateColumns="False" 
                  HorizontalAlignment="Stretch" 
                  VerticalScrollBarVisibility="Auto" 
                  VerticalAlignment="Bottom" 
                  CanUserAddRows="false" 
                  Width="Auto" 
                  MaxHeight="216" 
                  LoadingRow="dataGrid_LoadingRow">
      <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding FullName, Mode=OneWay}" Header="Name"/>
                <DataGridTextColumn Binding="{Binding Address.Zip, Mode=OneWay}" Header="PLZ"/>
                <DataGridTextColumn Binding="{Binding Address.City, Mode=OneWay}" Header="ORT"/>
      </DataGrid.Columns>
</DataGrid>

Loading_Row的代码:

private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    try
    {
        TempObject temp = e.Row.DataContext as TempObject;

        if (temp.IsDuplicate)
        {
            e.Row.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0066ff"));
        }                     
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

已尝试解决方案:

e.Row.IsEnable = false; (dataGrid_LoadingRow)

问题:当我们尝试点击行时,它没有被选中。 但是当我选择其他有效行并按 Ctrl + A 时,所有行都被选中。

还有其他办法吗?请提供示例代码的答案。

0 个答案:

没有答案