2012-10-18 86 views
1

我在我的應用程序中有一個DataGrid控件,其中我爲某些操作添加了自定義的Popup。 基本的東西是:我有一個double數字顯示在DataGrid矩陣。右擊選中的單元格會彈出一個彈出窗口,用戶可以在其中鍵入一個值,然後按回車鍵移動所選值(所有選定的值將隨着輸入的值而遞增)如何在鼠標位置上方顯示彈出窗口?

這是當前行爲:

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能夠從一個二維數組顯示值):

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

現在,我需要一個ContextMenu添加到我的DataGrid。當我嘗試在DataGrid上直接添加ContextMenu時,我可以在網格初始化之前看到它,但之後僅顯示Popup。根本沒有ContextMenu。 我期待他們在相同的位置,但ContextMenu只是不會出現。

理想是什麼我需要的是一個類似Office的ContextMenu

Office popup

在其中我移彈出窗口將顯示鼠標指針上面,我的ContextMenu在通常位置。

如果需要,我不會在乎有一個固定的佈局(上面的佈局/ contextmenu下,無論鼠標位置)。

你有什麼想法如何做到這一點?

或者,我正在考慮在contextMenu中包含Popup內容,希望它不會變得醜陋。

感謝您的幫助!

+0

試圖通過將它的IsOpen屬性爲true,以手動打開文本菜單。此外,嘗試尋找StaysOpen屬性,如果它以某種方式幫助你:http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.staysopen.aspx – decyclone

+0

已經開始工作了,Popup到目前爲止工作得很好,我對這個Popup的唯一問題是將它的位置設置在鼠標位置上方(默認情況下,它位於鼠標位置下)。ContextMenu似乎是某種Popup在這裏的「重載」,可能是由於MouseRightButtonUp上的覆蓋:/ – Damascus

+0

你有沒有嘗試設置它的Placement屬性? http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.placement.aspx – decyclone

回答

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

哇。謝謝你,先生。這真的很棒!不完全是我所需要的(所以我仍然有我的「彈出式鼠標?」問題懸而未決),但我不在乎,這就像一個魅力!再次感謝! – Damascus

相關問題