DataGrid设置ReadOnly并允许滚动?

时间:2012-11-01 18:03:47

标签: c# wpf datagrid scroll readonly

我有DataGrid,当我不希望用户更改任何内容时,我当前设置为ReadOnly,然后我有一个按钮供用户单击以允许修改,然后将IsReadOnly更改为false

但现在问题是当DataGrid.IsReadOnly = true时,滚动被禁用。如何设置DataGrid IsReadOnly=true或类似网格,并且仍能滚动DataGrid

DataGrid风格:

<DataGrid.Style>
  <Style TargetType="{x:Type DataGrid}">
    <Style.Triggers>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ReadOnly}" Value="True"/>
          <Condition Binding="{Binding Reordering}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter Property="IsReadOnly" Value="True" />
          <Setter Property="IsHitTestVisible" Value="False" />
          <Setter Property="Foreground" Value="Gray" />
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ReadOnly}" Value="True"/>
          <Condition Binding="{Binding Reordering}" Value="True"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter Property="IsHitTestVisible" Value="False" />
          <Setter Property="IsReadOnly" Value="True" />
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding ReadOnly}" Value="False"/>
        </MultiDataTrigger.Conditions>
        <MultiDataTrigger.Setters>
          <Setter Property="IsHitTestVisible" Value="True" />
          <Setter Property="IsReadOnly" Value="False" />
        </MultiDataTrigger.Setters>
      </MultiDataTrigger>
    </Style.Triggers>
  </Style>
</DataGrid.Style>

1 个答案:

答案 0 :(得分:0)

你试过这个:

dataGridView1.ReadOnly = true;
void dataGridView1_DoubleClick(object sender, EventArgs e)
{
     if (dataGridView1.ReadOnly == true)
          return;

     // .. whatever code you have in your handler...
}

Disable DataGridView except the scroll