代码之家  ›  专栏  ›  技术社区  ›  svick Raja Nadar

禁用在WPF DataGrid中选择

  •  42
  • svick Raja Nadar  · 技术社区  · 16 年前

    如何在WPFTooklit中禁用选择 DataGrid 我试着修改对你有用的解决方案 ListView (来自 WPF ListView turn off selection ),但这行不通:

    <tk:DataGrid>
        <tk:DataGrid.ItemContainerStyle>
            <Style TargetType="{x:Type tk:DataGridRow}">
                <Setter Property="Focusable" Value="false"/>
            </Style>
        </tk:DataGrid.ItemContainerStyle>
        <tk:DataGrid.CellStyle>
            <Style TargetType="{x:Type tk:DataGridCell}">
                <Setter Property="Focusable" Value="false"/>
            </Style>
        </tk:DataGrid.CellStyle>
    </tk:DataGrid>
    
    10 回复  |  直到 9 年前
        1
  •  -9
  •   viky    16 年前

    这有一个诀窍。 您可以处理DataGrid(例如dgGrid)的SelectionChanged事件,并在处理程序中写入:

    dgGrid.UnselectAll();
    

        2
  •  42
  •   JiBéDoublevé    12 年前

    最简单的方法是,重写行和单元格的样式

    <DataGrid.Resources>
        <ResourceDictionary>
            <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{x:Null}" />
                        <Setter Property="BorderBrush" Value="{x:Null}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{x:Null}" />
                        <Setter Property="BorderBrush" Value="{x:Null}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ResourceDictionary>
    </DataGrid.Resources>
    
        3
  •  24
  •   Community Mohan Dere    8 年前

    要完全禁用DataGrid中的行选择,可以执行以下操作:

    <DataGrid>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="IsHitTestVisible" Value="False"/>
            </Style>
        </DataGrid.RowStyle>
        <!--Other DataGrid items-->
    </DataGrid>
    

    这可能被认为比设置更有利 <Setter Property="IsEnabled" Value="False"/> 因为执行上述技术会导致行的样式发生更改。它也不会禁止右键单击时出现上下文菜单。

    最后:重要的是要注意“设置” IsHitTestVisible 至“假”禁用 与行的交互,包括编辑。

    但是,如果您只想更改选定行的样式,请查看答案 here .

        4
  •  21
  •   ybonda    12 年前

    IsHitTestVisible="False" DataGrid

        5
  •  11
  •   Rhyous    15 年前

    以上这些都是简单黑客的好主意。然而,他们并没有完全按照要求去做。其他答案告诉我们如何取消选择用户选择的内容,或者隐藏用户选择的内容。

    不过,我明白为什么会给出这些答案。提供真正的解决方案并不容易。

    真正的解决方案是首先防止选择,这并不简单,但只需几个简单的步骤就可以做到。

    回答 1.必须在Expression Blend中复制样式(或在某处找到样式的副本)。

    如果您需要更多详细信息,或者需要深入了解,请参阅我的博客:

    How to disable row selection in a WPF DataGrid?

        6
  •  8
  •   Community Mohan Dere    9 年前

    正如Sonic Soul所指出的 here ,viky的解决方案实际上不起作用。

    下面是在数据网格中禁用选择的实际工作代码:

    grid.SelectionChanged += (obj, e) => 
      Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => 
        grid.UnselectAll())); 
    
        7
  •  5
  •   Mindaugas-kun    8 年前

    我发现唯一正确的方法就是禁用 DataGrid上的属性 风格

    尽管命名错误,单击事件仍将注册。确保不要在整个DataGrid上更改此属性,除非还希望禁用滚动。

    一种干净的方法是在静态资源中使用新样式(根据需要复制其他setter)

            <Style x:Key="DataGridUnselectableRowStyle" TargetType="{x:Type DataGridRow}">
                <Setter Property="IsHitTestVisible" Value="False"/>
            </Style>
    

    然后把它绑定到你的数据网格上

            <DataGrid
                RowStyle="{StaticResource DataGridUnselectableRowStyle}" >
                <!-- Contents -->
            </DataGrid>
    
        8
  •  4
  •   Mr. Snuggles    15 年前

        9
  •  4
  •   Vadim Ovchinnikov    9 年前

    如果使用其他颜色:

    <Style TargetType="{x:Type DataGrid}">
        <Setter Property="RowBackground" Value="#badeee"/>
        <Setter Property="AlternationCount" Value="2" />
        <Setter Property="AlternatingRowBackground" Value="#92cce5"/>
    </Style>
    
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>           
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    
    <Style TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                <Setter Property="Background" Value="#badeee"></Setter>
                <Setter Property="BorderBrush" Value="#badeee"></Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                <Setter Property="Background" Value="#92cce5"></Setter>
                <Setter Property="BorderBrush" Value="#92cce5"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    
        10
  •  2
  •   Vipin Gupta    11 年前

    我们要求禁用datagrid上的几行,但同时允许在这些行上使用箭头键导航。这就是为什么我们必须切换到'ishitsetvisible'而不是控制'IsEnabled'属性。因此,我们无法采用上述切换到“IsEnable”属性的解决方案。

    下面是我如何解决这个问题的。我为DataGridRow创建了一个新的附加属性(RowEnable)。此附加属性可以绑定到viewmodel属性以控制“虚拟”启用和禁用。我还为DataGridCell创建了一个新样式,基于相同的viewmodel属性将“ishitsetvisible”设置为false。因此,把它看作是一个鼠标/键盘可以看到的行,但是看不到它的单元格/列。这意味着现在我可以根据新的附加属性(RowEnabled)设置行的样式,使其看起来禁用/启用。同时,我可以查看这些实际上被禁用的行的工具提示。

        11
  •  1
  •   Piano Telope    5 年前
    <DataGrid isEnabled="False">
    </DataGrid>
    

    这是直接回答问题的最简单方法:禁用wpfdatagrid中的选择。

        12
  •  0
  •   marsh-wiggle    6 年前

    我需要一个代码隐藏的解决方案。这对我很有用:

    controlGrid.SelectedCellsChanged += (sender, e) =>
        Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
            controlGrid.UnselectAll()));
    controlGrid.Columns.Clear();
    

    因为它有时会闪烁,所以BackgroundProperty也可以设置为transparent。

    Style dataGridCellStyle = new Style(typeof(DataGridCell));
    Setter newSetterCell = new Setter(DataGridCell.BackgroundProperty, Brushes.Transparent);
    dataGridCellStyle.Setters.Add(newSetterCell);
    controlGrid.CellStyle = dataGridCellStyle;
    
    推荐文章