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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With