Reputation: 1932
What I'm trying to do here is set the visibility of a MenuItem based on whether a certain tab is selected on a TabControl, the 'View' menu in this case. My preference would be to avoid doing this in code-behind, which I'm sure must be possible.
I'll Post what I've got, but it's obviously wrong.
<Window x:Class="CallEntryTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Call Entry" Height="720" Width="800">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<DockPanel>
<Menu Height="27" DockPanel.Dock="Top" HorizontalAlignment="Stretch" Name="menu1" VerticalAlignment="Top">
<MenuItem Header="_File" >
<MenuItem Header="_Open Customer File" />
<MenuItem Header="E_xit" />
</MenuItem>
<MenuItem Header="_View"
Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabItem}}}"
>
<MenuItem Header="_Refresh">
<MenuItem Header="_Incremental" />
<MenuItem Header="_Full" />
</MenuItem>
</MenuItem>
<MenuItem Header="_Help">
<MenuItem Header="_About" />
</MenuItem>
</Menu>
<StatusBar DockPanel.Dock="Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Stretch">
<StatusBarItem Content="Customer File.bpcl"></StatusBarItem>
<Separator/>
<StatusBarItem Content="Server Status"></StatusBarItem>
</StatusBar>
<TabControl x:Name="_mainTabControl">
<TabItem x:Name="_callEntryTab" Header="Customer Call Entry">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem x:Name="_geoDisplayTab" Header="Geographic Display">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem x:Name="_callsTab" Header="Customer Calls">
<Grid x:Name="_customerCallsTab" Background="#FFE5E5E5" />
</TabItem>
<TabItem x:Name="_unassocCallsTab" Header="Unassociated Calls">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</DockPanel>
</Window>
The first obvious problem is I'm trying to bind the 'View' menu item to the 'IsSelected' property of a TabItem, but I don't know how to specify which TabItem.
The second problem is that the TabControl is not an ancestor of the MenuItem, so the FindAncestor call won't work.
A lot of the on-line articles that solve similar (but not the same) problems change the DataContext of the containers. This will be an MVVM app, so the DataContext will be my view-model, right?
Apologies for what is probably something that I should have been able to figure out myself.
Upvotes: 1
Views: 419
Reputation: 1932
I must be tired. Here's the solution.
<MenuItem Header="_View"
Visibility="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}, ElementName=_callsTab}"
>
Time for a nap...
Upvotes: 1