Spongebob Comrade
Spongebob Comrade

Reputation: 1568

MenuItem template change based on bound data

Basically I have a View which is bound to a ViewModel which has MenuItems.

What I want to do is that whenever the menu title is "-" I want to place a separator.

Theoretically it seems that I can avoid TemplateSelectors but if you think that it's inevitable please share even those solutions.

Here is the XAML:

<Window x:Class="WpfApp1.MenuItemStyle"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MenuItemStyle" Height="300" Width="300">
    <DockPanel>
        <Menu DockPanel.Dock="Top" ItemsSource="{Binding MenuItems}">
            <Menu.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Title}" Value="-">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                                        <Separator />
                                    </HierarchicalDataTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>

                    <TextBlock Text="{Binding Title}" Background="Red" />
                </HierarchicalDataTemplate>
            </Menu.ItemTemplate>

        </Menu>
        <Grid>
        </Grid>
    </DockPanel>
</Window>

And here is the code behind:

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MenuItemStyle.xaml
    /// </summary>
    public partial class MenuItemStyle : Window
    {
        public MenuItemStyle()
        {
            InitializeComponent();

            this.DataContext = this;
        }

        public ObservableCollection<MenuItem> MenuItems { get; set; } = new ObservableCollection<MenuItem> {
            new MenuItem{ Title = "M1"
                ,Children= new ObservableCollection<MenuItem>{
                    new MenuItem{ Title = "M2"},
                    new MenuItem{ Title = "-"},
                    new MenuItem{ Title = "M3"},
                }
            }
        };
    }

    public class MenuItem
    {
        public string Title { get; set; }   
        public ObservableCollection<MenuItem> Children { get; set; }
    }
}

I have searched everywhere for a solution but couldn't find a pragmatic one.

Upvotes: 4

Views: 208

Answers (1)

dymanoid
dymanoid

Reputation: 15197

You can create a Style for the MenuItems. Make it either local for a concrete menu instance (by placing in the menu's Resources) or place it in your resource dictionary:

<Menu ItemsSource="{Binding MenuItems}">
    <Menu.Resources>
        <Style TargetType="MenuItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Title}" Value="-">
                    <Setter Property="Template">
                         <Setter.Value>
                            <ControlTemplate>
                                <Separator/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Menu.Resources>
    <Menu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Title}" Background="Red" />
        </HierarchicalDataTemplate>
    </Menu.ItemTemplate>
</Menu>

Upvotes: 1

Related Questions