简体   繁体   中英

Binding if a DataGrid column is a tab stop in WPF

I have a DataGridTextColumn that needs to be a tab stop sometimes, and not a tab stop sometimes.

I have

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

And that works just fine in preventing the column from being tabbed to.

I want to do something like

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

Where IsSomeColumnTabStop is a boolean in the DataGrid 's DataContext . Unfortunately... this does not work!

I have also tried

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

But no luck!

I ended up doing this in code behind in a way that feels pretty dirty... binding would definitely be preferred if anybody can point me in the right direction!

In my DataGrid 's Resources I defined

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

Then when I want tab stops, I can do...

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

And for no tab stops...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM