在WPF DataGrid中禁用第一行的第一列 [英] Disable First Column of First Row in WPF DataGrid
问题描述
是否有一种方法(使用样式和多重触发器)始终禁用WPF控件中的 DataGrid
第一行的第一列?这是模板列,在编辑模式下显示组合框,在正常模式下显示文本框。我希望它永远不要进入编辑模式(仅此列)。该行中的其余列应该可以进入编辑模式。
Is there a way (using styles and multi triggers) to always disable the first column of the first row of a DataGrid
in a WPF control? This is a templated column which shows combo box in edit mode and text box in normal mode. I'd like this to never go into the edit mode (only this column). The rest of the columns in the row should be able to go into the edit mode.
推荐答案
是的,有可能(在换句话说,您想禁用某个确定的单元格),但我最好使用 ValueConverter
或 CellTemplateSelector
达到目标。
在下面的限制中,您必须为<$设置 AlternationCount
属性c $ c> DataGrid 到ItemsSource中元素的数量。
这是变通以获取行索引,因为 DataGridRow
没有获取索引的属性(只有方法 GetIndex()
)。
Yes, it is possible (in other words you want to disable a definite cell), but I would preferable use ValueConverter
or CellTemplateSelector
to reach the goal.
In the soution below there is a restriction, that you have to set an AlternationCount
property for the DataGrid
to the number of the elements in ItemsSource.
This is a "work around" to get the row index, since DataGridRow
has no property to get the index(only a method GetIndex()
).
<DataGrid ItemsSource="{Binding YourItemsCollection}" AutoGenerateColumns="false" AlternationCount="{Binding YourItemsCollection.Count}">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Column.DisplayIndex, RelativeSource={RelativeSource Self}}" Value="0"/>
<Condition Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="0"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEnabled" Value="False" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
这篇关于在WPF DataGrid中禁用第一行的第一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!