sfaust
sfaust

Reputation: 2373

WPF Design Time Data in Menu Subitem Style

I have a WPF project with a menu. One of the menus has subitems dynamically generated by binding to a collection in code behind. Here is the XAML for this item:

<MenuItem Header="Open Files" Name="MiInsertOpen"
        d:DataContext="{d:DesignInstance Type={x:Type core:DBInterface}}"
        ItemsSource="{Binding InsertableDBs}">
    <MenuItem.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding DisplayName}" />
            <Setter Property="ToolTip" Value="{Binding FilePath}" />
            <Setter Property="Command" Value="{Binding DataContext.CmdInsert, 
                    RelativeSource={RelativeSource FindAncestor, 
                    AncestorType=MenuItem, AncestorLevel=2}}" />
            <Setter Property="CommandParameter" Value="{Binding FilePath}" />
        </Style>
    </MenuItem.ItemContainerStyle>
</MenuItem>

This all works fine at run time and I get what I would expect, etc. The data context of each subitem is the proper DB that it represents.

At design time, however, the parent MenuItem has it's design time data context properly set and recognizes 'IsertableDBs', but in the style it's complaining that it can't find 'DisplayName' and the other properties on type 'DBInterface' (the DataContext type for the parent menu). How do I tell it that the items in that style should have DataContext of the DB type?

Upvotes: 0

Views: 119

Answers (1)

sfaust
sfaust

Reputation: 2373

Ok after googling some more I found the answer here. This seems to be working; the only thing I have to add is that the d: tag comes up empty in Intellisense when entering it; just type in 'Style.DataContext' manually and it works...

Upvotes: 0

Related Questions