将上下文菜单显示为全屏WPF

时间:2017-03-07 11:09:40

标签: c# wpf mvvm

我想显示一个填满整个屏幕的上下文菜单。

我基本上想在屏幕上的项目上方显示弹出窗口。

这是我必须显示上下文菜单的代码:

<Button Height="23" HorizontalAlignment="Left" Name="button1"
        VerticalAlignment="Top" Width="33" Click="button1_Click" >
  <Button.Content>
    <Path x:Name="BtnArrow" Margin="4" VerticalAlignment="Center" Width="10"
          Fill="#FF527DB5" Stretch="Uniform" HorizontalAlignment="Right"
          Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z "/>
  </Button.Content>
  <Button.ContextMenu>
    <ContextMenu>
      <MenuItem Header="First" Foreground="Snow">
        <MenuItem.Icon>
          <Image Source="../Images/Button/detailsIcon.png"></Image>
        </MenuItem.Icon>
      </MenuItem>

      <MenuItem Header="Second" Foreground="Snow">
        <MenuItem.Icon>
          <Image Source="../Images/Button/detailsIcon.png"></Image>
        </MenuItem.Icon>
      </MenuItem>

      <MenuItem Header="Third" Foreground="Snow">
        <MenuItem.Icon>
          <Image Source="../Images/Button/detailsIcon.png"></Image>
        </MenuItem.Icon>
      </MenuItem>

      <MenuItem Header="Fourth" Foreground="Snow">
        <MenuItem.Icon>
          <Image Source="../Images/Button/detailsIcon.png"></Image>
        </MenuItem.Icon>
      </MenuItem>                       
    </ContextMenu>
  </Button.ContextMenu>
</Button>

3 个答案:

答案 0 :(得分:0)

使用此代码:

Window.xaml设计:

<Window.Resources>
    <ContextMenu x:Key="popuptrigger">
        <MenuItem Header="First" Foreground="Snow">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Second" Foreground="Snow">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Third" Foreground="Snow">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Fourth" Foreground="Snow">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</Window.Resources>
<Grid>
    <Button Content="Click Me" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.56,0.45" Margin="105,100,0,0" Click="Button_Click"/>
</Grid>

<强> Window.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
    {
        ContextMenu cm=this.FindResource("popuptrigger") as ContextMenu;
        cm.PlacementTarget = sender as Button;
        cm.IsOpen = true;
    }

答案 1 :(得分:0)

试用此代码:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="{x:Static SystemParameters.PrimaryScreenHeight}" Width="{x:Static SystemParameters.PrimaryScreenWidth}" WindowStartupLocation="CenterScreen">
<Window.Resources>
    <ContextMenu x:Key="cmButton" Height="{x:Static SystemParameters.PrimaryScreenHeight}" Width="{x:Static SystemParameters.PrimaryScreenWidth}">
        <MenuItem Header="First" Foreground="BlueViolet">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Second" Foreground="Chocolate">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Third" Foreground="ForestGreen">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>

        <MenuItem Header="Fourth" Foreground="Maroon">
            <MenuItem.Icon>
                <Image Source="../Images/Button/detailsIcon.png"></Image>
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</Window.Resources>
<Grid>
    <Button Content="Click Me" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.56,0.45" Margin="109,61,0,0" Click="Button_Click"/>

</Grid>

答案 2 :(得分:0)

我设法使用以下样式做到了这一点:

<Style TargetType="{x:Type ContextMenu}">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <Border Background="Black" CornerRadius="7, 7, 8, 8" BorderBrush="Transparent" BorderThickness="2" >
                            <StackPanel ClipToBounds="True" Orientation="Vertical" IsItemsHost="True" Margin="5,4,5,4"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>