Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putting ContextMenu in WPF Styles

I am trying to put ContextMenu in styles with below so that it can be shared by all textboxes.

<Grid.Resources>
            <Style x:Key="Cell" TargetType="TextBox">
                <Setter Property="Margin" Value="0"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="BorderThickness" Value="0.2"/>
                <Setter Property="VerticalAlignment" Value="Stretch"/>
                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                <Setter Property="MinHeight" Value="30"/>
                <Setter Property="MinWidth" Value="70"/>
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu>
                            <MenuItem Header="Level1" Click="SubLevel1_Click_1"/>
                            <MenuItem Header="SubLevel1"/>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>

        </Grid.Resources>

This code works fine till I add Click event. When click event is added it Builds but throws following an exception when it is executed: 'Set connectionId threw an exception.' Line number '21' and line position '34'.

Am i doing something wrong here.

like image 259
indra Avatar asked Mar 15 '23 23:03

indra


1 Answers

Not sure what's causing this, but you can easily workaround this by putting ContextMenu as a separate resource:

<ContextMenu x:Key="ContextMenu">
    <MenuItem Header="Level1" Click="SubLevel1_Click_1"/>
    <MenuItem Header="SubLevel1"/>
</ContextMenu>
<Style TargetType="TextBox">
    <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
</Style>
like image 114
Athari Avatar answered Mar 23 '23 16:03

Athari