1

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

这是当前的行为:

移位弹出

以及弹出窗口的 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能够显示二维数组中的值):

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

现在,我需要在ContextMenu我的DataGrid. 当我尝试ContextMenu直接在 上添加 a 时DataGrid,我可以在网格初始化之前看到它,但之后只Popup显示。完全没有ContextMenu。我原以为他们会在同一个位置,但ContextMenu只是不会出现。

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

办公室弹出

其中,我的 shift Popup 将出现在鼠标指针上方,而 myContextMenu在通常的位置。

如果需要,我不在乎有一个固定的布局(上面的布局/下面的上下文菜单,无论鼠标位置是什么)。

你对如何做到这一点有任何想法吗?

或者,我正在考虑将Popup内容包含在 contextMenu 中,希望它最终不会变得丑陋。

谢谢你的帮助!

4

1 回答 1

1

这个想法是覆盖默认ContextMenu模板。

结果:

在此处输入图像描述

添加引用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>
于 2012-10-19T04:31:08.170 回答