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

WPF Data binding when using templates and styles

acordner
1#
acordner Published in 2018-01-13 07:32:31Z

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.

Menu

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)

<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>
acordner
2#
acordner Reply to 2018-01-15 16:37:24Z

Dave M provided a link in the comments above to a solution to this problem. Thanks Dave!

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO