I have a WPF user control. In that userControl I'll have multmple WPF childControls(inherits MyBaseElement
).
我有一个WPF用户控件。在那个userControl中,我将拥有多个WPF childControls(继承MyBaseElement)。
I need that every ChildControl contains a specified context menu (backward, forward commands)
我需要每个ChildControl都包含一个指定的上下文菜单(向后,向前命令)
The code bellow does not work... What I do wrong?
下面的代码不起作用......我做错了什么?
<UserControl.Resources>
<ContextMenu x:Key="ElementContextMenu">
<MenuItem Header="Move backward"
Name="back"
Click="back_Click" />
<MenuItem Header="Move forward"
Name="forw"
Click="forw_Click" />
</ContextMenu>
<Style TargetType="{x:Type my:BaseElement}">
<Setter Property="ContextMenu"
Value="{DynamicResource ResourceKey=ElementContextMenu}" />
1 个解决方案
#1
1
A base style isn't applied automatically to derived elements, you need to create a style for each one. Fortunately you can use style inheritance through the BasedOn
property.
基本样式不会自动应用于派生元素,您需要为每个元素创建样式。幸运的是,您可以通过BasedOn属性使用样式继承。
<UserControl.Resources>
<Style
TargetType="{x:Type my:Element1DerivingFromBase}"
BasedOn="{StaticResource {x:Type my:BaseElement}}" />
<Style
TargetType="{x:Type my:Element2DerivingFromBase}"
BasedOn="{StaticResource {x:Type my:BaseElement}}" />
#1
1
A base style isn't applied automatically to derived elements, you need to create a style for each one. Fortunately you can use style inheritance through the BasedOn
property.
基本样式不会自动应用于派生元素,您需要为每个元素创建样式。幸运的是,您可以通过BasedOn属性使用样式继承。
<UserControl.Resources>
<Style
TargetType="{x:Type my:Element1DerivingFromBase}"
BasedOn="{StaticResource {x:Type my:BaseElement}}" />
<Style
TargetType="{x:Type my:Element2DerivingFromBase}"
BasedOn="{StaticResource {x:Type my:BaseElement}}" />