1

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.

2 Answers 2

1

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)
{

}
Sign up to request clarification or add additional context in comments.

Comments

0

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"

1 Comment

How would the MyClass need to look? I find all the command examples on the internet quite confusing...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.