修改可编辑组合框 (WPF) 的上下文菜单

Modifying a context menu for the editable combobox (WPF)

我需要通过向菜单项添加图标图像来修改内置组合框上下文菜单(复制、剪切、粘贴)。

我将所需的上下文菜单添加到 ComboBox 控件模板内的 PART_EditableTextBox,并将该模板作为资源包含在内。

 <TextBox x:Name="PART_EditableTextBox"
               Style="{x:Null}"
               Template="{StaticResource ComboBoxTextBox}"
               HorizontalAlignment="Left"
               VerticalAlignment="Bottom"
               Margin="3,3,23,3"
               Focusable="True"
               Background="Transparent"
               Visibility="Hidden"
               IsReadOnly="{TemplateBinding IsReadOnly}">
                        <TextBox.ContextMenu>
                            <ContextMenu>
                                <MenuItem Header="Copy"
                                          Command="ApplicationCommands.Copy">
                                    <MenuItem.Icon>
                                        <Image Source="pack://application:,,,/testApp.UI;component/ViewModels/PngImages/Copy.ico" Style="{StaticResource ResourceKey=ImageStyleSmall}" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Cut"
                                          Command="ApplicationCommands.Cut">
                                    <MenuItem.Icon>
                                        <Image Source="pack://application:,,,/testApp.UI;component/ViewModels/PngImages/Cut.ico" Style="{StaticResource ResourceKey=ImageStyleSmall}" />
                                    </MenuItem.Icon>
                                </MenuItem>
                                <MenuItem Header="Paste"
                                          Command="ApplicationCommands.Paste">
                                    <MenuItem.Icon>
                                        <Image Source="pack://application:,,,testApp.UI;component/ViewModels/PngImages/Paste.ico" Style="{StaticResource ResourceKey=ImageStyleSmall}" />
                                    </MenuItem.Icon>
                                </MenuItem>
                            </ContextMenu>
                        </TextBox.ContextMenu>
                    </TextBox>

当应用程序运行时,上下文菜单按计划工作,但副作用是我的组合框失去了边框。

你的模板"ComboBoxTextBox"怎么样?你那里有边界吗?

以下是默认文本框样式的开始部分的声明方式:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>

通过说 Style="{x:Null}" ,您基本上告诉框架 "I don't want any of that!"

注意你缺少的这两行:

<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
<Setter Property="BorderThickness" Value="1"/>

其他缺少的属性也可能会破坏某些选项卡导航、与设备的交互、拖放等

在我最初的测试项目中,我使用了从 MSDN 站点(位于 docs.microsoft.com/en-us/dotnet/framework/wpf/controls/…)复制的控件模板。然后我采用了不同的方法:创建一个小应用程序,添加组合框并选择编辑模板选项。对该模板进行了类似的更改并使其正常工作。我的上下文菜单和组合框边框都完好无损。