2017-09-29 72 views
0

我需要通過向菜單項添加圖標圖像來修改內置組合框上下文菜單(複製,剪切,粘貼)。修改可編輯組合框(WPF)的上下文菜單

我將所需的上下文菜單添加到組合框控件模板中的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> 

當應用程序運行時,上下文菜單按計劃運行,但副作用是我的組合框丟失了邊框。

回答

0

在我的原始測試項目我使用了我從MSDN站點複製的控件模板(位於docs.microsoft.com/en-us/dotnet/framework/wpf/controls/...)。然後我採取了不同的方法:創建了一個小應用程序,添加了組合框並選擇了編輯模板選項。對該模板進行了類似的更改並使其可以正常工作。我有兩個上下文菜單工作和組合框邊框完好無損。

0

你的模板「ComboBoxTextBox」呢?你有邊界嗎?

+0

我還沒有修改該模板,也沒有任何其他。 ComboBoxTextBox模板有一個邊框元素。 –

0

下面是如何默認文本框的風格的開始部分聲明:

<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"/> 

按說風格=「{x:空}」,你基本上告訴框架「我不希望有任何的那!」

注意這兩條線,你所缺乏的:

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

其他缺少的屬性可能會打破一些標籤導航,與設備的交互,拖&下降等,以及

+0

我遵循了這個建議,但沒有幫助。 –