Click here to Skip to main content
Click here to Skip to main content

WPF Menu using ViewModel - Part 1

By , 18 Jun 2009
 
<Window x:Class="MenuItemViewModelSample.MenuSampleWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:MenuViewModel;assembly=MenuViewModel"
    Title="Window1" Height="302" Width="441">
    <Grid>
        <StackPanel>
            <Menu>
                <MenuItem ItemsSource="{Binding Path=ChildMenuItems}" Header="{Binding Path=Header}">
                    <MenuItem.Resources>
                        <HierarchicalDataTemplate DataType="{x:Type vm:MenuItemViewModel}" ItemsSource="{Binding ChildMenuItems}">
                            <MenuItem Header="{Binding Path=Header}"/>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type vm:SeparatorViewModel}">
                            <Separator>
                                <Separator.Template>
                                    <ControlTemplate>
                                        <Line X1="0" X2="1" Stroke="Black" StrokeThickness="1" Stretch="Fill"/>
                                    </ControlTemplate>
                                </Separator.Template>
                            </Separator>
                        </DataTemplate>
                    </MenuItem.Resources>
                </MenuItem>
            </Menu>
        </StackPanel>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Visu.v
Software Developer (Senior) Multi National
India India
Member
I'm a C# developer with a multinational company. Recently started working on wpf and i'm hooked to it completely now.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 19 Jun 2009
Article Copyright 2009 by Visu.v
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid