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

时间:2017-09-29 14:09:45

标签: wpf combobox contextmenu

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

我在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>

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

3 个答案:

答案 0 :(得分:0)

你的模板“ComboBoxTextBox”怎么样?你有边境吗?

答案 1 :(得分:0)

下面是如何声明默认TextBox样式的开头部分:

<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}”,你基本上告诉框架“我不想要任何一个!”

请注意您缺少的这两行:

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

其他缺失的属性可能会破坏一些标签导航,与设备的互动,拖放和放大放弃等等

答案 2 :(得分:0)

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

相关问题