Disable First Column of First Row in WPF DataGrid

淺唱寂寞╮ 提交于 2019-12-02 13:40:00

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!