2017-03-04 9 views
0

은이 같은 두 개의 열이있는 상황에 맞는 메뉴를 만들려고하고 상황에 맞는 메뉴 항목에서 두 열 키는 GestureText 속성에 바인딩됩니다. 그러나, 열 정의는 순종하지 않는 것 그리고 나는이 참조 :WPF : 항목의 텍스트가 항목 및 바로 가기의 이름 속성을 바인딩</p> <p><a href="https://i.stack.imgur.com/Nrfqw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nrfqw.png" alt="Context Menu"></a></p> <p>:

<Window x:Class="ContextMenu2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid Margin="0,2,0.4,-2.2"> 
     <TextBlock Text="RightClickMe" HorizontalAlignment="Left" VerticalAlignment="Top"> 
      <TextBlock.ContextMenu> 
       <ContextMenu ItemsSource="{Binding Items}"> 
        <ContextMenu.ItemTemplate> 
         <DataTemplate> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="50*"/> 
            <ColumnDefinition Width="70*"/> 
           </Grid.ColumnDefinitions> 
           <TextBlock Grid.Column="0" Text="{Binding Name}" /> 
           <TextBlock Grid.Column="1" Text="{Binding GestureText}"/> 
          </Grid> 
         </DataTemplate> 
        </ContextMenu.ItemTemplate> 
       </ContextMenu> 
      </TextBlock.ContextMenu> 
     </TextBlock> 
    </Grid> 
</Window> 

C#을

using System; 
using System.Collections.ObjectModel; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Input; 

namespace ContextMenu2 
{ 
    public class CMenu 
    { 
     public String Name { get; set; } 
     public string GestureText { get; set; } 
    } 

    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = this; 
     } 

     public ObservableCollection<CMenu> Items { get; set; }= new ObservableCollection<CMenu> 
     { 
      new CMenu() 
      { 
       Name = "Press me", 
       GestureText = 
        new KeyGesture(Key.Q, ModifierKeys.Control).GetDisplayStringForCulture(CultureInfo.CurrentUICulture) 
      }, 
      new CMenu() 
      { 
       Name = "Press me too", 
       GestureText = 
        new KeyGesture(Key.R, ModifierKeys.Control).GetDisplayStringForCulture(CultureInfo.CurrentUICulture) 
      } 
     }; 
    } 
} 

여기에 어떤 문제가 다음과 같이 enter image description here

내 XAML은?

답변

0

나는 이것을 테스트하지는 않았지만 내 생각 엔 열을 포함하는 MenuItems가 전체 너비를 차지하지 않는다는 것입니다. 아마도 왼쪽 정렬 일 것입니다. 따라서 정렬을 변경해보십시오 (예 : 이 같은 : 당신은 GestureTextItemContainerStyleInputGestureText을 결합 할 수

<ContextMenu.ItemContainerStyle> 
    <Style TargetType="{x:Type MenuItem}"> 
     <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    </Style> 
</ContextMenu.ItemContainerStyle> 
1

:

<ContextMenu ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch"> 
    <ContextMenu.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Name}" /> 
     </DataTemplate> 
    </ContextMenu.ItemTemplate> 
    <ContextMenu.ItemContainerStyle> 
     <Style TargetType="MenuItem"> 
      <Setter Property="InputGestureText" Value="{Binding GestureText}"/> 
     </Style> 
    </ContextMenu.ItemContainerStyle> 
</ContextMenu> 

또한 HorizontalAlighnment 또는 HorizontalContentAlignment를 설정하면 도움이 될 것되지 않습니다. default style:

<Grid.ColumnDefinitions> 
    <ColumnDefinition Width="Auto" 
         SharedSizeGroup="Icon" /> 
    <ColumnDefinition Width="*" /> 
    <ColumnDefinition Width="Auto" 
         SharedSizeGroup="Shortcut" /> 
    <ColumnDefinition Width="13" /> 
</Grid.ColumnDefinitions> 

의이 부분과 Margin="5,2,2,2" 제 3 열에서 TextBlock에의 마진을 유의하시기 바랍니다. 당신이 정말로 ContextMenu을 찾고 정의를 만들려면

0

당신은 즉 정의의 MenuItem ItemContainer의 ControlTemplate를 오버라이드 (override) 할 필요가 당신의 대신이 하나의 ControlTemplate에서 대신의 ContextMenu의 ItemTemplate에서 Grid :

<TextBlock Text="RightClickMe" HorizontalAlignment="Left" VerticalAlignment="Top"> 
    <TextBlock.ContextMenu> 
     <ContextMenu ItemsSource="{Binding Items}"> 
      <ContextMenu.ItemContainerStyle> 
       <Style TargetType="MenuItem"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type MenuItem}"> 
           <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> 
            <Grid Margin="-1"> 
             <Grid.ColumnDefinitions> 
              <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/> 
              <ColumnDefinition Width="13"/> 
              <ColumnDefinition Width="*"/> 
              <ColumnDefinition Width="5*" SharedSizeGroup="a"/> 
              <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/> 
              <ColumnDefinition Width="7*" SharedSizeGroup="b"/> 
             </Grid.ColumnDefinitions> 
             <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/> 
             <Border x:Name="GlyphPanel" BorderBrush="#FF26A0DA" BorderThickness="1" Background="#3D26A0DA" ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" Visibility="Hidden" VerticalAlignment="Center" Width="22"> 
              <Path x:Name="Glyph" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Height="11" Width="10"/> 
             </Border> 
             <ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" 
                     Content="{Binding Name}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/> 
             <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" 
                   Text="{Binding GestureText}" VerticalAlignment="Center" HorizontalAlignment="Right"/> 
            </Grid> 
           </Border> 
           <ControlTemplate.Triggers> 
            <Trigger Property="Icon" Value="{x:Null}"> 
             <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
            </Trigger> 
            <Trigger Property="IsChecked" Value="True"> 
             <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/> 
             <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/> 
            </Trigger> 
            <Trigger Property="IsHighlighted" Value="True"> 
             <Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/> 
             <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/> 
            </Trigger> 
            <Trigger Property="IsEnabled" Value="False"> 
             <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/> 
             <Setter Property="Fill" TargetName="Glyph" Value="#FF707070"/> 
            </Trigger> 
            <MultiTrigger> 
             <MultiTrigger.Conditions> 
              <Condition Property="IsHighlighted" Value="True"/> 
              <Condition Property="IsEnabled" Value="False"/> 
             </MultiTrigger.Conditions> 
             <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/> 
             <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/> 
            </MultiTrigger> 
           </ControlTemplate.Triggers> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ContextMenu.ItemContainerStyle> 
     </ContextMenu> 
    </TextBlock.ContextMenu> 
</TextBlock> 

GestureText 속성에 InputGestureText 속성을 바인딩하려면 @ Ron의 대답을 참조해야합니다.