0

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
  • 193
  • 8
  • In a ListView, use `ListViewItem` instead of `ListBoxItem` as the TargetType of an ItemContainerStyle. That said, a "ViewModel for the UserControl, and is assigned to the DataContext property" is generally a bad idea. A UserControl should never have its own view model, and you should never explicitly assign its DataContext property. Doing so usually breaks the DataContext-related Bindings to the UserControl's properties. Instead of an own view model, the control should expose bindable properties. See here: https://stackoverflow.com/a/25673948/1136211 – Clemens Jan 13 '18 at 08:18
  • Thanks for the info. I will change the ListBoxItem to a ListViewItem (full disclosure - code was originally written by someone else, i'm just trying to update a few things). Regarding the ViewModel, I am using Caliburn.Micro, so I am not actually explicitly assigning the ViewModel to the DataContext of the View (UserControl). I can't redesign it at this point, so I have to move forward. – acordner Jan 13 '18 at 17:34
  • Possible duplicate: https://stackoverflow.com/questions/3668654/relativesource-binding-from-a-tooltip-or-contextmenu – Dave M Jan 15 '18 at 02:04
  • Dave M - That was the key! Thank you! – acordner Jan 15 '18 at 16:35

1 Answers1

0

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

acordner
  • 193
  • 8