开发者

WPF - TabControl - Prevent Selection Change

It seems that the WPF TabControl doesn't support the ability to cancel a selection change, since there is no SelectionChanging() event, only a SelectionChanged event. Has anyone figured out a way to d开发者_JAVA技巧o this?

The only way I have found is to attach to the PreviewMouseLeftButtonDown() event on each TabItem and set e.Handled to true if I don't want that particular page selected. This seems to work but is clunky.


I found a way do do this using a style for a TabItem and then binding the Focusable property to a boolean that controls the behavior. Getting a binding to the parent view model was a bit wonky but that could probably be improved.

This is nice because it avoids tricks with click events which may not get fired if the user uses the keyboard for example.

<TabControl.Resources>
    <Style TargetType="{x:Type TabItem}">
        <Setter Property="Focusable"  Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.IsUpToDate}" />
        <Setter Property="HeaderTemplate">
             <Setter.Value>
                <DataTemplate>
                    <TextBlock Text="{Binding Group}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</TabControl.Resources>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜