views:

369

answers:

2

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.

+1  A: 

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}" />
Andy
Andy -- brilliant, thanks for your quick and effective response. I had to make one change to your code to support my purposes (removed the IsCheckable setter and add a Click handler to focus the appropriate TabItem) but otherwise it worked perfectly. I'd vote the answer helpful but I do not have enough reputation yet (just a newbie). Thanks.
Malcolm
Actually, the proper response is to mark this answer as accepted, so show that you've found the answer you were looking for. Click the check mark under the number of votes to accept the answer.
Andy
Thanks, Andy. Newbie strikes again... ;)
Malcolm
+2  A: 

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.

micahtan
Thanks -- this worked great.
Malcolm