0

当我右键单击数据网格时,我有上下文菜单。它完全符合我的要求,但问题是,这种菜单样式会覆盖现有的菜单样式。我只想将 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");

谢谢迪

4

1 回答 1

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>
于 2012-11-10T08:25:33.703 回答