Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / WPF

Creating Menus (with Drop-Down Sub-Menus) using Images in WPF

Rate me:
Please Sign up or sign in to vote.
4.36/5 (7 votes)
29 Nov 2012CPOL6 min read 33.9K   18  
Creating Menus (with Drop-Down Sub-Menus) using Images in WPF.
<Window x:Class="VisualMenus.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <BitmapImage x:Key="SmileImage" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="Images\CheeseSmile.gif" />
        <BitmapImage x:Key="RockThisImage" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="Images\RockThis.png" />
        <BitmapImage x:Key="VacationImage" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="Images\Vacation.png" />
        <BitmapImage x:Key="FBLikeImage" CreateOptions="IgnoreImageCache" CacheOption="OnLoad" UriSource="Images\fblike.jpg" />
    </Window.Resources>
    <Grid>
        <Menu Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Width="517">
            <MenuItem x:Name="_smile" Click="_smile_Click_1" Height="30" Width="30" HorizontalAlignment="Left" HorizontalContentAlignment="Stretch">
                <MenuItem.Icon>
                    <Image Source="{DynamicResource SmileImage}" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem x:Name="_rockthis" Click="_rockthis_Click_1" Height="30" Width="30" HorizontalAlignment="Left">
                <MenuItem.Icon>
                    <Image Source="{DynamicResource RockThisImage}" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem x:Name="_vacation" Height="30" Width="30" HorizontalAlignment="Left">
                <MenuItem.Icon>
                    <Image Source="{DynamicResource VacationImage}" />
                </MenuItem.Icon>
                <MenuItem.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </MenuItem.ItemsPanel>
                <MenuItem x:Name="_tahiti" Header="Go To Tahiti" Click="_tahiti_Click_1" >
                    <MenuItem.Icon>
                        <Image Source="{DynamicResource VacationImage}" Height="20" Width="20" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem x:Name="_belize" Header="Dive Belize" Click="_belize_Click_1" >
                    <MenuItem.Icon>
                        <Image Source="{DynamicResource VacationImage}" Height="20" Width="20" />
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem x:Name="_fresno" Header="In What World is Fresno Better Than Paris?" Click="_fresno_Click_1" >
                    <MenuItem.Icon>
                        <Image Source="{DynamicResource VacationImage}" Height="20" Width="20" />
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem x:Name="_fblike" Click="_fblike_Click_1" Height="30" Width="30" HorizontalAlignment="Left">
                <MenuItem.Icon>
                    <Image Source="{DynamicResource FBLikeImage}" />
                </MenuItem.Icon>
            </MenuItem>
        </Menu>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service 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)


Written By
Software Developer (Senior)
United States United States
Shanan has been developing software professionally since 1994. She has had her hands in all aspects of design and development, from embedded systems to user interfaces and everything in between. She has worked equally across her career in Unix and Windows based systems, and has coded in many different languages. At this point in her career, Shanan is a senior .NET developer.

Outside work, Shanan is a mother of two young kids, and she is a fiction writer. She also plays the flute and the hammered dulcimer.

Comments and Discussions