Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2589
Binding XamMenuItem to data
posted

Hello,

Here is my code:

 

<Menus:XamMenuItem x:Name="mnuChartType" ItemsSource="{Binding GalleryTypes}">
                        <Menus:XamMenuItem.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="20"/>
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding Converter={StaticResource ctImageConv}}" Grid.Column="0" Height="14" Width="15" Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                <TextBlock Text="{Binding}" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                            </Grid>
                        </DataTemplate>
                    </Menus:XamMenuItem.ItemTemplate>
                </Menus:XamMenuItem>

In application menu looks like this. And question is on picture :) How do I put icons on right places?

 

And almost forgot. How do I subscribe to click events on each menu item in databound menus?

Thank you.

  • 6475
    Verified Answer
    posted

    Hi,

    You should set the image to the Icon property of the children. So, your code should look something like this:

    <Menus:XamMenuItem x:Name="mnuChartType" ItemsSource="{Binding GalleryTypes}">
        <Menus:XamMenuItem.DefaultItemsContainer>
            <DataTemplate>
                <Menus:XamMenuItem>
                    <Menus.XamMenuItem.Icon>
                        <Image Source="{Binding Converter={StaticResource ctImageConv}}" />
                    </Menus.XamMenuItem.Icon>
                </Menus:XamMenuItem>
            </DataTemplate>
        </Menus:XamMenuItem.DefaultItemsContainer>
    </Menus:XamMenuItem>
    

     

    As for your second question, I'd recommend using the ItemClicked event of the Xammenu instead of subscribing for the Click event of each MenuItem.

    Hope this helps,