【问题标题】:Show popup with binding on DataGrid row mouse over在 DataGrid 行鼠标上显示绑定的弹出窗口
【发布时间】:2016-06-28 20:34:22
【问题描述】:

当用户在 DataGrid 中悬停一行时,我想显示一个弹出窗口,其中包含有关该行的一些信息。 我坚持如何将 DataTrigger 绑定到动态填充的 DataGrid 表中的每一行。

我找到了仅针对工具提示的解决方案,但工具提示不适合我,因为我需要对弹出窗口进行更多控制(当用户将鼠标光标移动到另一个控件时不要立即隐藏它,能够单击弹出窗口用鼠标等)

这是我试图将弹出 DataTrigger 绑定到每个 DataGrid 行的 XAML 代码(我在下面的代码中放置了带有问题的 cmets)

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

请帮我弄清楚怎么做。

【问题讨论】:

  • 你找到解决办法了吗?

标签: wpf xaml binding datagrid datatrigger


【解决方案1】:
  1. 创建 2 个 AttachedProperty,分别称为 RowPopupShowPopupRowPopup 将持有对 Popup 控件的引用,ShowPopup 将根据 DataGridRow.IsMouseOver 属性显示/隐藏此 Popup。这些都非常容易实现。

  2. 使用 TargetType DataGridRow 创建一个 Style

例子,

xmlns:local="clr-namespace: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}" ... />

附加属性代码:

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);
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多