Bind Command to MenuItem

Posted by Neir0 on Stack Overflow See other posts from Stack Overflow or by Neir0
Published on 2010-04-24T21:17:22Z Indexed on 2010/04/24 21:23 UTC
Read the original article Hit count: 359

Filed under:
|
|

Hi

I have ListView and i am trying to bind command to ContextMenu of ListView.

<ListView x:Name="listView1" ItemsSource="{Binding Path=Persons}">
            <ListView.Resources>
                <ContextMenu x:Key="ItemContextMenu">
                    <MenuItem Header="Add" />
                    <MenuItem Header="Edit"/>
                    <Separator/>
                    <MenuItem Header="Delete" Command="{Binding Msg}" /> 
                </ContextMenu>
            </ListView.Resources>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <!--<EventSetter Event="PreviewMouseLeftButtonDown" />--><!--Handler="OnListViewItem_PreviewMouseLeftButtonDown" />-->
                    <Setter Property="ContextMenu" Value="{StaticResource ItemContextMenu}"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
                    <GridViewColumn Header="Sur Name" DisplayMemberBinding="{Binding Path=SurName}" />
                    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Path=Age}" />
                </GridView>
            </ListView.View>


        </ListView>
        <Button Content="Message" Command="{Binding Msg}" />

Binding to Button works well but when i click to delete item in ContextMenu, command is not working! Why?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wpf