Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向上下文菜单添加控件_C#_Wpf_Xaml_Contextmenu_Menuitem - Fatal编程技术网

C# 向上下文菜单添加控件

C# 向上下文菜单添加控件,c#,wpf,xaml,contextmenu,menuitem,C#,Wpf,Xaml,Contextmenu,Menuitem,我有一个上下文菜单,我想添加一些控件也。在下面的示例中,我添加了一个文本框、复选框和一个滑块 <ContextMenu> <MenuItem Header="Cut" Command="Cut" /> <MenuItem Header="Copy" Command="Copy" /> <MenuItem Header="Paste" C

我有一个上下文菜单,我想添加一些控件也。在下面的示例中,我添加了一个文本框、复选框和一个滑块

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

其结果是:

有没有办法让这个看起来更好

我可以禁用菜单项周围的可选蓝色边框(显示为红色)吗

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


您的
边框
被包装在
菜单项
中。您可以使用
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>