如果DataGrid列是WPF中的制表位,则绑定

时间:2014-10-21 15:51:42

标签: c# wpf datagrid

我有一个DataGridTextColumn,有时需要一个制表位,有时候不是制表符。

我有

<DataGridTextColumn x:Name="SomeColumn">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="KeyboardNavigation.IsTabStop" Value="False" />
                </Style>
            </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

这样可以很好地防止列被标记为。

我想做点什么

<Setter Property="KeyboardNavigation.IsTabStop" Value="{Binding IsSomeColumnTabStop}" />

其中IsSomeColumnTabStopDataGrid&#39; s DataContext中的布尔值。不幸的是......这不起作用!

我也试过

<Setter Property="KeyboardNavigation.IsTabStop" Value="{Binding DataContext.IsSomeColumnTabStop}" />

但没有运气!

1 个答案:

答案 0 :(得分:0)

我最后以一种感觉非常脏的方式在代码中执行此操作...如果有人能指出我正确的方向,那么绑定肯定会更受欢迎!

在我定义的DataGrid Resources

<Style x:Key="TabStopColumn" TargetType="DataGridCell">
    <Setter Property="KeyboardNavigation.IsTabStop" Value="True"/>
</Style>

<Style x:Key="NoTabStopColumn" TargetType="DataGridCell">
    <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>

然后,当我想停止标签时,我可以做...

SomeColumn.CellStyle = (Style)MyDataGrid.Resources["TabStopColumn"];

没有标签停止......

SomeColumn.CellStyle = (Style)MyDataGrid.Resources["NoTabStopColumn"];