2016-03-13 39 views
1

當用戶在DataGrid中懸停行時,我想顯示一個彈出窗口,其中包含有關此行​​的一些信息。 我堅持如何將DataTrigger綁定到動態填充的DataGrid表中的每一行。在DataGrid行鼠標上彈出綁定DataGrid行鼠標的彈出框

我已經找到解決方案只爲工具提示,但工具提示不適合我,因爲我需要有更多的彈出控制(不要在用戶將鼠標光標移動到另一個控件時立即隱藏它,能夠點擊鼠標等彈出)

這裏就是我試圖彈出DataTrigger綁定到每一個DataGrid行XAML代碼(我已經把意見用下面的代碼問題)

<Window x:Class="VKPN.UI.Windows.TestWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:VKPN.UI.Windows" 
     mc:Ignorable="d" 
     Title="TestWindow" SizeToContent="WidthAndHeight"> 
    <Grid> 
     <Popup Name="UserPopup" Placement="RelativePoint" HorizontalOffset="-5" VerticalOffset="0" 
            PlacementTarget="{Binding ElementName=ThisUserControl}"> 
      <Popup.Style> 
       <Style TargetType="Popup"> 
        <Style.Triggers> 
         <!--How to specify binding to every DataGridTable row below?--> 
         <DataTrigger Binding="{Binding ElementName=DataGridTable, Path=???}" Property="IsMouseOver" Value="True"> 
          <Setter Property="IsOpen" Value="True"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </Popup.Style> 
      <Label> 
       <Label.Style> 
       <Style TargetType="Label"> 
        <Style.Triggers> 
         <!--How to specify binding to every DataGridTable row below?--> 
         <DataTrigger Binding="{Binding ElementName=???}" Property="IsMouseOver" Value="True"> 
          <!--DataGrid row has a column "id" which I want to show in the label. Did I do it correct below?--> 
          <Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext.id}"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
       </Label.Style> 
      </Label> 
     </Popup> 
      <DataGrid Name="DataGridTable" ItemsSource="{Binding}" IsReadOnly="True" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto"> 
      </DataGrid> 
    </Grid> 
</Window> 

請幫我找出如何去做。

+0

你已經找到了一些解決方案? –

回答

0
  1. 創建2個AttachedProperty稱爲RowPopupShowPopupRowPopup將持有對Popup控件的引用,並且ShowPopup將根據DataGridRow.IsMouseOver屬性顯示/隱藏此Popup。這些非常容易實現。

  2. 創建一個Style與TargetType DataGridRow

例,

的xmlns:地方= 「CLR的命名空間:myNameSpace對象」

<Style TargetType="DataGridRow" x:Key="RowStyleKey"> 
     <Setter Property="local:CustomADP.RowPopup" Value="{Binding ElementName=Popup1}"/> 
     <Setter Property="local:CustomADP.ShowPopup" Value="{Binding IsMouseOver, Mode=OneWay, RelativeSource={RelativeSource Self}}"/> 
</Style> 

<DataGrid RowStyle="{StaticResource RowStyleKey}" ... /> 

AttachedProperties代碼:

public class CustomADP 
    { 
     /********* Set Popup to show ****************/ 

     public static Popup GetRowPopup(DependencyObject obj) 
     { 
      return (Popup)obj.GetValue(RowPopupProperty); 
     } 

     public static void SetRowPopup(DependencyObject obj, Popup value) 
     { 
      obj.SetValue(RowPopupProperty, value); 
     } 

     public static readonly DependencyProperty RowPopupProperty = 
      DependencyProperty.RegisterAttached("RowPopup", typeof(Popup), typeof(CustomADP), new PropertyMetadata(null));    

     /************* Show Hide using IsOpen property ************/ 

     public static bool GetShowPopup(DependencyObject obj) 
     { 
      return (bool) obj.GetValue(ShowPopupProperty); 
     } 

     public static void SetShowPopup(DependencyObject obj, bool value) 
     { 
      obj.SetValue(ShowPopupProperty, value); 
     } 

     // Using a DependencyProperty as the backing store for ShowPopup. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty ShowPopupProperty = 
      DependencyProperty.RegisterAttached("ShowPopup", typeof(bool), typeof(CustomADP), new PropertyMetadata(false, new PropertyChangedCallback(ShowPopupCallback))); 

     private static void ShowPopupCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     {   
      if (!(d is DataGridRow)) 
       return; 

      if (((DataGridRow)d).IsFocused == true) 
      {    
       Popup p = CustomADP.GetRowPopup(d); 
       p.IsOpen = Convert.ToBoolean(e.NewValue); 
      } 
      else 
      { 
       Popup p = CustomADP.GetRowPopup(d); 
       p.IsOpen = Convert.ToBoolean(e.NewValue); 
      } 
     } 
    } 
相關問題