向Contextmenu添加控件

时间:2014-11-12 11:28:55

标签: c# wpf xaml contextmenu menuitem

我有一个上下文菜单,我想添加一些控件。在下面的例子中,我添加了一个TextBox,CheckBox和一个Slider。

<ContextMenu>
    <MenuItem Header="Cut"
                Command="Cut" />
    <MenuItem Header="Copy"
                Command="Copy" />
    <MenuItem Header="Paste"
                Command="Paste" />
    <Separator />

    <Border Background="#999"
            BorderThickness="1"
            BorderBrush="Black"
            Padding="5">
        <Grid Width="300">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="8" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>

            <Label Content="PropA"
                    Grid.Column="0"
                    Grid.Row="0" />
            <Label Content="PropB"
                    Grid.Column="0"
                    Grid.Row="1" />
            <Label Content="PropC"
                    Grid.Column="0"
                    Grid.Row="2" />

            <TextBox Text="-10"
                        Grid.Column="2"
                        Grid.Row="0" />
            <CheckBox Grid.Column="2"
                        Grid.Row="1" />
            <Slider Grid.Column="2"
                    Grid.Row="2" />
        </Grid>
    </Border>
</ContextMenu>  

结果是:

enter image description here

有没有让这个看起来更好看?

我可以禁用MenuItem周围的可选蓝色边框(以红色显示)吗?

enter image description here

我可以拉伸控件以适应菜单吗?

enter image description here

1 个答案:

答案 0 :(得分:3)

您的Border包含在MenuItem中。您可以使用MenuItem并将Border移动为其模板

<ContextMenu>
   <MenuItem Header="Cut" Command="Cut" />
   <MenuItem Header="Copy" Command="Copy" />
   <MenuItem Header="Paste" Command="Paste" />
   <Separator />
   <MenuItem>
      <MenuItem.Template>
         <ControlTemplate TargetType="{x:Type MenuItem}">
            <Border Background="#999" BorderThickness="1" BorderBrush="Black" Padding="5">
               <Grid Width="300">
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="auto" />
                     <ColumnDefinition Width="8" />
                     <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>
                  <Grid.RowDefinitions>
                     <RowDefinition Height="auto" />
                     <RowDefinition Height="auto" />
                     <RowDefinition Height="auto" />
                  </Grid.RowDefinitions>
                  <Label Content="PropA" Grid.Column="0" Grid.Row="0" />
                  <Label Content="PropB" Grid.Column="0" Grid.Row="1" />
                  <Label Content="PropC" Grid.Column="0" Grid.Row="2" />
                  <TextBox Text="-10" Grid.Column="2" Grid.Row="0" />
                  <CheckBox Grid.Column="2" Grid.Row="1" />
                  <Slider Grid.Column="2" Grid.Row="2" />
               </Grid>
            </Border>
         </ControlTemplate>
      </MenuItem.Template>
   </MenuItem>
</ContextMenu>