Peter
Peter

Reputation: 249

Static context menu command target

I have the following context menu on the header of a column in a datagridview.

<DataGridCheckBoxColumn Binding="{Binding Include,UpdateSourceTrigger=PropertyChanged}"  Width="50">
    <DataGridCheckBoxColumn.HeaderTemplate>
        <DataTemplate>
            <TextBlock Text="Export">
                <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Alle auswaehlen"/>
                        <MenuItem Header="Alle abwahelen"/>
                    </ContextMenu>
                </TextBlock.ContextMenu>      
            </TextBlock>
        </DataTemplate>
    </DataGridCheckBoxColumn.HeaderTemplate>
</DataGridCheckBoxColumn>

As you can see, the context menu is static. How can I map the Command attribute to static methods in my code? All examples I found online were for flexible binding or for cut/copy.

Upvotes: 1

Views: 151

Answers (2)

Jan
Jan

Reputation: 2168

You could use the click event instead:

<MenuItem Header="Alle auswaehlen" Click="MenuItem_Click_1"/>

And then have this method in your code:

private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{

}

Upvotes: 1

dlev
dlev

Reputation: 48606

I don't see why that ContextMenu would be static; it appears to be created for each text block, which will be created for each header. If you only have one header, then I suppose it's de facto static.

Either way, if you want to bind to static command MyCommand of class MyNamespace.MyClass, then you use the following syntax:

<MenuItem Header="header" Command="{x:Static mynamespace:MyClass.MyCommand}"/>

Note that you need to have specified the xml namespace on the parent XAML object as follows:

xmlns:mynamespace="clr-namespace:MyNamespace"

Upvotes: 0

Related Questions