wpf - 在 DataGrid 行鼠标悬停时显示带有绑定(bind)的弹出窗口

标签 wpf xaml binding datagrid datatrigger

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

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

这是 XAML 代码,我尝试将弹出 DataTrigger 绑定(bind)到每个 DataGrid 行(我在下面的代码中添加了带有问题的注释)

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

请帮我弄清楚该怎么做。

最佳答案

  1. 创建 2 个名为 RowPopupShowPopupAttachedPropertyRowPopup 将保存对 Popup 控件的引用,并且 ShowPopup 将根据 显示/隐藏此 Popup DataGridRow.IsMouseOver 属性。这些非常容易实现。

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

关于wpf - 在 DataGrid 行鼠标悬停时显示带有绑定(bind)的弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976795/

相关文章:

c# - 将按位枚举映射到 sql 列值

c# - 对话框有未请求的额外空间

c# - 单击文本框后删除文本

Angular - 父组件到子组件的绑定(bind)函数

c# - 有没有办法让 XAML/C# StringFormat 显示较大金额的整美元,但较小金额的美分?

WPF:绑定(bind)到选定的 TreeViewItem

c# - 将列表数组添加到数据网格列中

c# - 如何从 ItemTemplate 中将命令绑定(bind)到 ContextMenu?

c# - 如何调用 UpdateSource() 以在 DataGrid 上进行显式绑定(bind)?

wpf - 如何单向绑定(bind)到 materialDesign :ButtonProgressAssist. IsIndicatorVisible DependencyProperty