C# PlacementTarget将弹出窗口放置到选定的DataGridRow

C# PlacementTarget将弹出窗口放置到选定的DataGridRow,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我想要一个弹出窗口,直接打开DataGrid中选定行的上方/下方 目前我有: <DataGrid x:Name="myDataGrid">...</DataGrid> <Popup IsOpen="{Binding ShowPopup}" PlacementTarget="{Binding ElementName=myDataGrid}" Placement="Bottom"

我想要一个弹出窗口,直接打开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]
(作为路径)不起作用,我正在寻找正确的PlacementTarget


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

所以我有DataGrid和DataGridRow风格

<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>
财产是重要的。它绑定到popop的IsOpen属性

以及主窗口后面的代码

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
不要忘记在构造函数->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;
        }
    }
希望,这有帮助

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;
}
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    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;
        }
    }
   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;
}