Home WPF Data binding when using templates and styles
Reply: 0

WPF Data binding when using templates and styles

user4067 Published in May 21, 2018, 9:17 am

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.


Visual Tree

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)

    <Style x:Key="EmptyButtonStyleWithExtraMenuImage" TargetType="{x:Type Button}">
        <Setter Property="ContextMenu">
                <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}"/>
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type Button}">
                    <Image Name="ExtraMenuImage" Source="..."/>

    <Style x:Key="OptionsListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border >
                            <DockPanel LastChildFill="True" Margin="0,10,10,10">
                                <ContentPresenter DockPanel.Dock="Left"/>
                                <Button x:Name="buttonControl" 
                                        Style="{StaticResource ResourceKey=EmptyButtonStyleWithExtraMenuImage}" 

<ListView x:Name="SpecificationTablesUI" Style="{StaticResource OptionsListViewStyle}" 
        ItemsSource="{Binding Path=SpecTables}">
            <DockPanel LastChildFill="True">
                <Expander Header="{Binding TableName}" Style="{StaticResource StatusGroupExpander}">
                    <TextBlock Text="{Binding SelectedQuantity}"/>
        <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource OptionsListViewItemStyle}">
            <Setter Property="Tag" Value="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"/>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310804 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO