wpf ContextMenu样式

时间:2012-05-11 18:58:22

标签: wpf contextmenu styling

当我右键单击datagrid时,我有上下文菜单。它正是我想要的,但问题是,这个菜单样式覆盖了现有的菜单样式。我想仅将ControlTemplate应用于此菜单。请帮助。

这是XAML

<ContextMenu  x:Key="cellContextMenu">
    <MenuItem Header="Insert symbol" Margin="0,0,0,0" HorizontalContentAlignment="Right">
        <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
            <wpfc:SymbolController x:Name="dgSymbolControl" Width="400" Height="400" value="{Binding Path=SelectedSymbolItem, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}"></wpfc:SymbolController>
            <TextBlock FontFamily="DYMObvba" Text="{Binding ElementName=dgSymbolControl, Path=value}" />
        </StackPanel>
    </MenuItem>
</ContextMenu>
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
    <Grid>
        <Border Name="Check" Visibility="Collapsed" Margin="0,0,0,0"></Border>
        <ContentPresenter Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True"/>
    </Grid>
</ControlTemplate>

以下是应用菜单的代码:

eCell.ContextMenu = (ContextMenu)FindResource("cellContextMenu");

由于 DEE

1 个答案:

答案 0 :(得分:1)

我会尝试在ContextMenu中定义ControlTemplate:

<ContextMenu  x:Key="cellContextMenu">
<ContextMenu.Resources>
...
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
    <Grid>
        <Border Name="Check" Visibility="Collapsed" Margin="0,0,0,0"></Border>
        <ContentPresenter Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True"/>
    </Grid>
</ControlTemplate>
</ContextMenu.Resources>
    <MenuItem Header="Insert symbol" Margin="0,0,0,0" HorizontalContentAlignment="Right">
        <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
            <wpfc:SymbolController x:Name="dgSymbolControl" Width="400" Height="400" value="{Binding Path=SelectedSymbolItem, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}"></wpfc:SymbolController>
            <TextBlock FontFamily="DYMObvba" Text="{Binding ElementName=dgSymbolControl, Path=value}" />
        </StackPanel>
    </MenuItem>
</ContextMenu>

...或在XAML中设置MenuItem的样式:

<ContextMenu x:Key="cellContextMenu">
    <MenuItem Style="MyMenuStyle" Header="Insert symbol" Margin="0,0,0,0" HorizontalContentAlignment="Right">
        <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
            <wpfc:SymbolController x:Name="dgSymbolControl" Width="400" Height="400" value="{Binding Path=SelectedSymbolItem, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}"></wpfc:SymbolController>
            <TextBlock FontFamily="DYMObvba" Text="{Binding ElementName=dgSymbolControl, Path=value}" />
        </StackPanel>
    </MenuItem>
</ContextMenu>

<Style x:Key="MyMenuStyle" TargetType="{x:Type MenuItem}">
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Grid>
            <Border Name="Check" Visibility="Collapsed" Margin="0,0,0,0"></Border>
            <ContentPresenter Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True"/>
        </Grid>
    </ControlTemplate>
</Style>