弹出窗口的PlacementTarget到选定的DataGridRow

时间:2014-12-05 13:21:10

标签: c# .net wpf xaml

我希望弹出窗口直接在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">

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

由于

1 个答案:

答案 0 :(得分:1)

我从未解决过这个问题。但现在,我试图模拟你的问题。

所以我有DataGrid和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>

这是弹出窗口。通过PlacementRectangle和Placement,您可以调整定位。

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

好吧,这个Style添加到你的DataGrid.Resources段,DataGrid应该是这样的

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

现在代码背后。我使用类Person作为我要显示的项目

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

选择属性非常重要。它绑定到popoup的IsOpen属性。

MainWindow背后的代码

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

不要忘记在构造函数中将DataContext指定给 this - &gt; 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;
        }
    }

这是事件处理程序,它为每个选定的项和未选择的项设置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;
}

希望,这有帮助。