将ContextMenu放入WPF样式 [英] putting ContextMenu in WPF Styles
本文介绍了将ContextMenu放入WPF样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将ContextMenu设置为下面的样式,以便可以被所有文本框共享.
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>
直到我添加Click事件,此代码才能正常工作.添加click事件时,它会生成,但在执行时会引发异常:设置connectionId引发异常."行号"21"和行位置"34".
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.
推荐答案
不确定是什么原因造成的,但是您可以通过将 ContextMenu
作为单独的资源来轻松解决此问题:
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>
这篇关于将ContextMenu放入WPF样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文