弹出到所选数据网格行的放置目标

本文关键字:目标 网格 数据网 数据 | 更新日期: 2023-09-27 18:33:24

我希望在 DataGrid 中所选行的正上方/下方打开一个弹出窗口。

目前我有:

<DataGrid x:Name="myDataGrid">...</DataGrid>
  <Popup IsOpen="{Binding ShowPopup}"
               PlacementTarget="{Binding ElementName=myDataGrid}"
               Placement="Bottom"
               StaysOpen="False"
               PopupAnimation="Slide"
               AllowsTransparency="True"
               FocusManager.IsFocusScope="False">

我想,我必须为PlacementTarget绑定设置一个Path。由于SelectedCells[0](作为路径)不起作用,因此我正在寻找正确的放置目标。

谢谢

弹出到所选数据网格行的放置目标

我从来没有解决这个问题。但是现在,我试图模拟你的问题。

所以我有 DataGrid with DataGridRow Style

<Style x:Key="DataGridRowStyle1" TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
                <Setter Property="ValidationErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridRow}">
                            <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <SelectiveScrollingGrid>
                                    <SelectiveScrollingGrid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </SelectiveScrollingGrid.ColumnDefinitions>
                                    <SelectiveScrollingGrid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="Auto"/>
                                    </SelectiveScrollingGrid.RowDefinitions>

这是弹出窗口。通过放置矩形和放置,您可以调整定位。

                                    <Popup IsOpen="{Binding IsSelected}" Placement="Left" PlacementRectangle="20,20,0,0"   >
                                        <TextBlock Text="Yessss this is my popup" Background="AliceBlue" Foreground="Black" />
                                    </Popup>
                                    <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                    <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                    <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                                </SelectiveScrollingGrid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsNewItem" Value="True">
                        <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

好吧,这种样式添加到您的 DataGrid.Resources 段中,DataGrid 应如下所示

<DataGrid x:Name="peopleDataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding People}" SelectionChanged="peopleDataGrid_SelectionChanged" RowStyle="{DynamicResource DataGridRowStyle1}" >

现在是后面的代码。我使用类人作为我要显示的项目

public class Person:INotifyPropertyChanged
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    private bool _IsSelected;
    public bool IsSelected
    {
        get { return _IsSelected; }
        set { 
            _IsSelected = value;
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("IsSelected"));
        }
    }
   public event PropertyChangedEventHandler PropertyChanged;
}

属性已选择很重要。它绑定到弹出窗口的 IsOpen 属性。

以及主窗口的代码隐藏

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

不要忘记在构造函数中将 DataContext 分配给 ->这个。数据上下文 = 这个;

    public List<Person> People
    {
        get {
            List<Person> retVal = new List<Person>();
            retVal.Add(new Person() { Id = 1, FirstName = "John", LastName = "Lenon" });
            retVal.Add(new Person() { Id = 2, FirstName = "Ringo", LastName = "Star" });
            retVal.Add(new Person() { Id = 3, FirstName = "Paul", LastName = "Mc Cartney" });
            retVal.Add(new Person() { Id = 4, FirstName = "George", LastName = "Harrison" });
            return retVal;
        }
    }

下面是事件处理程序,它为每个选定项和未选定项设置 IsSelected 属性

   private void peopleDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        foreach (object ro in e.RemovedItems)
        {
            Person rp = ro as Person;
            if(rp != null)
                rp.IsSelected = false;
        }
        foreach (object so in e.AddedItems)
        {
            Person sp = so as Person;
            if (sp != null)
                sp.IsSelected = true;
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

希望,这有帮助。