如何在鼠标位置上方显示Popup?

时间:2012-10-18 20:21:51

标签: c# wpf popup contextmenu

我的应用程序中有一个DataGrid控件,我在其中为某些操作添加了自定义Popup。 基本的事情是:我在double中显示了DataGrid个数字矩阵。右键单击选定的单元格将弹出一个弹出窗口,用户可以在其中键入一个值,然后按Enter键移动所选的值(所选的所有值将随输入的值递增)

以下是当前行为:

Shift popup

弹出窗口的XAML代码:

<Style x:Key="ShiftPopupStyle" TargetType="{x:Type Popup}">
    <Setter Property="IsOpen" Value="False" />
    <Setter Property="StaysOpen" Value="False" />
    <Setter Property="AllowsTransparency" Value="True" />
    <Setter Property="PopupAnimation" Value="Fade" />
    <Setter Property="Placement" Value="Mouse" />
    <Setter Property="Child">
        <Setter.Value>
            <Border BorderBrush="Navy" Background="AliceBlue" BorderThickness="1" CornerRadius="2">
                <StackPanel Orientation="Horizontal" Margin="5">
                    <TextBox Text="{Binding ShiftValue, UpdateSourceTrigger=PropertyChanged}" Width="30" Margin="4" >
                        <TextBox.InputBindings>
                            <KeyBinding Command="{Binding ShiftCommand}" Key="Enter" />
                        </TextBox.InputBindings>
                    </TextBox>
                    <Button Content="Shift" Margin="0,4,0,4"
                            Command="{Binding ShiftCommand}"/>
                </StackPanel>
            </Border>
        </Setter.Value>
    </Setter>
</Style>

以下是我打开Popup的方法,此方法位于我的自定义DataGrid中(我使用自定义DataGrid能够显示2D数组中的值):

protected override void OnMouseRightButtonUp(System.Windows.Input.MouseButtonEventArgs e)
{
    if (!this.IsReadOnly)
    {
        this.shiftPopup.IsOpen = true;
    }
    base.OnMouseRightButtonUp(e);
}

现在,我需要向ContextMenu添加DataGrid。当我尝试在ContextMenu上直接添加DataGrid时,我可以在网格初始化之前看到它,但之后只显示Popup。完全没有ContextMenu。 我希望他们在同一个地方,但ContextMenu不会出现。

理想情况下,我需要的是类似Office ContextMenu

Office popup

我的移位弹出窗口将显示在鼠标指针上方,而ContextMenu位于通常位置。

如果需要,我不会关心固定布局(布局在/ contextmenu下面,无论鼠标位置如何)。

您对如何操作有任何想法吗?

或者,我正在考虑在contextMenu中包含Popup内容,希望它不会变得难看。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我们的想法是覆盖默认的ContextMenu模板。

结果:

enter image description here

添加对PresentationFramework.Aero.dll的引用并尝试以下代码:

<Window 
    x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:System="clr-namespace:System;assembly=mscorlib"
    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
    Title="MainWindow"
    Height="350" Width="525">

    <Window.Resources>
        <Style x:Key="MenuStyle" TargetType="{x:Type ContextMenu}" BasedOn="{StaticResource {x:Type ContextMenu}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <StackPanel>
                            <theme:SystemDropShadowChrome Name="Shdw2" Color="Transparent" SnapsToDevicePixels="true">
                                <Border Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}" 
                                        BorderThickness="{TemplateBinding BorderThickness}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBox Text="{Binding ShiftValue, UpdateSourceTrigger=PropertyChanged}"
                                                 Width="30" Margin="4">
                                            <TextBox.InputBindings>
                                                <KeyBinding Command="{Binding ShiftCommand}" Key="Enter" />
                                            </TextBox.InputBindings>
                                        </TextBox>
                                        <Button Content="Shift" Margin="0,4,0,4" Command="{Binding ShiftCommand}" />
                                    </StackPanel>
                                </Border>
                            </theme:SystemDropShadowChrome>

                            <theme:SystemDropShadowChrome Name="Shdw" Color="Transparent" SnapsToDevicePixels="true">
                                <Border Name="ContextMenuBorder"
                                        Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}">
                                    <Grid>
                                        <Rectangle Fill="#F1F1F1" HorizontalAlignment="Left" Width="28" Margin="2"
                                                   RadiusX="2" RadiusY="2" />
                                        <Rectangle HorizontalAlignment="Left" Width="1" Margin="30,2,0,2"
                                                   Fill="#E2E3E3" />
                                        <Rectangle HorizontalAlignment="Left" Width="1" Margin="31,2,0,2" Fill="White" />


                                        <ScrollViewer Name="ContextMenuScrollViewer" CanContentScroll="true"
                                                      Grid.ColumnSpan="2" Margin="1,0"
                                                      Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
                                            <Grid RenderOptions.ClearTypeHint="Enabled">
                                                <Canvas Height="0" Width="0" HorizontalAlignment="Left"
                                                        VerticalAlignment="Top">
                                                    <Rectangle
                                                        Height="{Binding ElementName=ContextMenuBorder,Path=ActualHeight}"
                                                        Width="{Binding ElementName=ContextMenuBorder,Path=ActualWidth}"
                                                        Fill="{Binding ElementName=ContextMenuBorder,Path=Background}" />
                                                </Canvas>
                                                <ItemsPresenter Name="ItemsPresenter"
                                                                Margin="{TemplateBinding Padding}" KeyboardNavigation.DirectionalNavigation="Cycle"
                                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                            </Grid>
                                        </ScrollViewer>
                                    </Grid>
                                </Border>
                            </theme:SystemDropShadowChrome>
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasDropShadow" Value="true">
                                <Setter TargetName="Shdw" Property="Margin" Value="0,0,5,5" />
                                <Setter TargetName="Shdw" Property="Color" Value="#71000000" />
                                <Setter TargetName="Shdw2" Property="Margin" Value="0,0,5,5" />
                                <Setter TargetName="Shdw2" Property="Color" Value="#71000000" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <DataGrid IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding}" />
            </DataGrid.Columns>

            <DataGrid.ContextMenu>
                <ContextMenu Style="{StaticResource MenuStyle}">
                    <MenuItem Header="Item 1"></MenuItem>
                    <MenuItem Header="Item 2"></MenuItem>
                    <MenuItem Header="Item 3"></MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>

            <System:String>One</System:String>
            <System:String>Two</System:String>
            <System:String>Three</System:String>
        </DataGrid>
    </Grid>
</Window>