Question

I'm using a TabControl as my main workspace in an application, and I'd like to add a "Window" menu item that lists the headers of open tabs. The active (i.e. - focused) tab should be checked.

I've tried using an ItemsTemplate as follows:

            <MenuItem Header="_Window" ItemsSource="{Binding ElementName=ux_workspace, Path=Items}">
            <MenuItem.ItemTemplate>
                <DataTemplate>
                    <MenuItem Header="{Binding Path=Header}" IsCheckable="True" IsChecked="{Binding IsFocused, Mode=OneWay}">
                </DataTemplate>
            </MenuItem.ItemTemplate>
        </MenuItem>

Each MenuItem is then "nested", so to speak, inside of another MenuItem, which really isn't the intended result (the checkbox is in the header area, and there is a separate border around the internal item).

Is there a better way to do this?

Thanks in advance.

Was it helpful?

Solution

While it seems like there should be a way to do this with templates, creating and using a Style seems to work:

<Style x:Key="TabMenuItem" TargetType="MenuItem">
    <Setter Property="Header" Value="{Binding Path=Header}" />
    <Setter Property="IsCheckable" Value="True" />
    <Setter Property="IsChecked" Value="{Binding Path=IsFocused, Mode=OneWay}" />
</Style>

<MenuItem Header="_Window"
    ItemsSource="{Binding ElementName=ux_workspace, Path=Items}"
    ItemContainerStyle="{StaticResource TabMenuItem}" />

OTHER TIPS

Malcolm, you'll want to use IsSelected instead of IsFocused when binding to the MenuItem.

If you do use IsSelected instead of IsFocused, you'll also be able to bind IsSelected with a Mode=TwoWay so that you don't have to use a Click handler to select the appropriate TabItem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top