I have an issue trying to establish data bindings to the IsEnabled properties of some MenuItem controls that are defined in a Style and applied to a Button. The Button is defined in a Template that is applied to ListViewItems for a List that is on a UserControl. The properties that I need to bind the IsEnabled property of the MenuItem controls to are members of the ViewModel for the UserControl, and is assigned to the DataContext property.
Let's call the properties on the ViewModel "IsMenuItem1Enabled", "IsMenuItem2Enabled", etc.
I have tried everything I can think of to establish the data binding, including using FindAncestor for types ListView, ListBoxItem, UserControl, and nothing has worked. Any help would be greatly appreciated!
Here is a stripped down version of the Styles, Templates and ListView:
(Edit: I updated the ListBoxItem to be a ListViewItem)
<UserControl.Resources>
<Style x:Key="EmptyButtonStyleWithExtraMenuImage" TargetType="{x:Type Button}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu >
<MenuItem Header="Duplicate Table" Style="{StaticResource MenuItemStyle}"/>
<MenuItem Header="Edit Table" Style="{StaticResource MenuItemStyle}"/>
<MenuItem Header="View Summary" Style="{StaticResource MenuItemStyle}"/>
<Separator Style="{StaticResource SeparatorStyle}"/>
<MenuItem Header="Delete Table" Style="{StaticResource MenuItemStyle}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Image Name="ExtraMenuImage" Source="..."/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="OptionsListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid>
<Border >
<DockPanel LastChildFill="True" Margin="0,10,10,10">
<ContentPresenter DockPanel.Dock="Left"/>
<Button x:Name="buttonControl"
Style="{StaticResource ResourceKey=EmptyButtonStyleWithExtraMenuImage}"
DockPanel.Dock="Right"/>
</DockPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<ListView x:Name="SpecificationTablesUI" Style="{StaticResource OptionsListViewStyle}"
ItemsSource="{Binding Path=SpecTables}">
<ListView.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True">
<Expander Header="{Binding TableName}" Style="{StaticResource StatusGroupExpander}">
<TextBlock Text="{Binding SelectedQuantity}"/>
</Expander>
</DockPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource OptionsListViewItemStyle}">
<Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>