Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 禁用在WPF数据网格中选择_C#_Wpf_Datagrid_Focus_Selection - Fatal编程技术网

C# 禁用在WPF数据网格中选择

C# 禁用在WPF数据网格中选择,c#,wpf,datagrid,focus,selection,C#,Wpf,Datagrid,Focus,Selection,如何在WPFTooklit的数据网格中禁用选择? 我尝试修改适用于ListView(从)的解决方案,但不起作用: <tk:DataGrid> <tk:DataGrid.ItemContainerStyle> <Style TargetType="{x:Type tk:DataGridRow}"> <Setter Property="Focusable" Value="false"/>

如何在WPFTooklit的数据网格中禁用选择? 我尝试修改适用于
ListView
(从)的解决方案,但不起作用:

<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>

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

dgGrid.UnselectAll();

它将取消选中所有选中的行,结果将是“未选中任何行”。

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

下面是在DataGrid中禁用选择的实际工作代码:

grid.SelectionChanged += (obj, e) => 
  Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => 
    grid.UnselectAll())); 

另一个简单的方法是使用IsSelected触发器将选择样式更改为透明。

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

Style dataGridCellStyle = new Style(typeof(DataGridCell));
Setter newSetterCell = new Setter(DataGridCell.BackgroundProperty, Brushes.Transparent);
dataGridCellStyle.Setters.Add(newSetterCell);
controlGrid.CellStyle = dataGridCellStyle;
然而,我理解为什么会给出这些答案。提供真正的解决方案并不容易

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

答复 1.您必须在Expression Blend中复制样式(或在某处找到样式的副本)。 2.更改单个ItemPresenter设置。对我来说,在ItemPresenter上设置ishitSetVisible=“False”就足够了

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


干净的方法是,只覆盖行和单元格的样式

<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>

如果您使用其他颜色:

<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>


只需将
ishitestvisible=“False”
添加到
DataGrid
定义中。

如果其他人也面临同样的问题,他们可能会发现这很有帮助

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


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

要完全禁用数据网格中的行选择,可以执行以下操作:

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

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

最后:需要注意的是,将“”设置为“False”将禁用与行的所有交互,包括编辑


但是,如果您只想在选中时更改行的样式,请查看答案。

我发现唯一正确的方法是禁用DataGrid行的IshitteVisible属性

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

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


然后将其绑定到数据网格上

        <DataGrid
            RowStyle="{StaticResource DataGridUnselectableRowStyle}" >
            <!-- Contents -->
        </DataGrid>

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

controlGrid.SelectedCellsChanged += (sender, e) =>
    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render, new Action(() =>
        controlGrid.UnselectAll()));
controlGrid.Columns.Clear();
因为它有时会闪烁,BackgroundProperty也可以设置为透明

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


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

不起作用:这起作用:+1这是正确的:)…无论如何,我的工作在我的情况下很好:)Nikhil的版本非常适合我。我可以直接在DataGrid中编辑单元格,并且整行保持未选中状态。谢谢Nikhil。您应该避免与代码隐藏/ViewModel中的视图逻辑交互,这也不太正确-它会停止在
DataGridCell
s上发生的单击,这通常是不可取的。同意。总有一天,我将不得不找到一个解决方案,禁用选择,但允许单击。这完全禁用了选择。我必须用“透明”替换“{x:Null}”,以保持选择的有效性。这是Snuggles先生建议的方法。仅供参考,别忘了在触发器中设置
,否则默认情况下,选择的文本将为白色。@JiBéDoublevé我只是必须这样做,但我没有使用硬编码值,而是将其放在DataGridCell的触发器中:
,从而将其设置为原来的值。奇怪的是,在列定义上显式设置颜色似乎也会消除颜色更改,而不需要设置程序。如果您希望保留默认样式的设置(例如,通过材质设计),则可以将其添加到
样式
标记:
BasedOn=“{StaticReso
<DataGrid isEnabled="False">
</DataGrid>