首页 > 解决方案 > 基于其他 DataGrid 中的 SelectedItem 在 DataGrid 中显示对象变量

问题描述

我在 WPF 中创建了一个小型示例应用程序来演示我当前的问题: 在我的应用程序中,我有两个 DataGrid。

不幸的是,我的第二个 DataGrid 没有更新。当我在第一个 DataGrid 中选择一个项目时显示任何内容,虽然我用谷歌搜索并阅读了很多东西,但我自己无法解决它。

这就是我在 DGUpdate-Application 中得到的:

带有 DataGrid、DataContext 等的 MainWindow:

<Window x:Class="DGUpdate.MainWindow"
        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:DGUpdate"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainWindow_ViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <DataGrid
            Grid.Column="0"
            ItemsSource="{Binding MyObeservableCollectionOfPerson}" 
            SelectedItem="{Binding Path=SelectedPerson, Mode=TwoWay}"
            AutoGenerateColumns="False"
            IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Last name" Width="Auto" Binding="{Binding LastName}"/>
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid
            Grid.Column="1"
            ItemsSource="{Binding SelectedPerson}" 
            AutoGenerateColumns="False"
            IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Last name" Width="Auto" Binding="{Binding SelectedPerson.LastName}"/>
                <DataGridTextColumn Header="First name" Width="Auto" Binding="{Binding SelectedPerson.FirstName}"/>
                <DataGridTextColumn Header="Age" Width="Auto" Binding="{Binding SelectedPerson.Age}"/>
                <DataGridTextColumn Header="Weight" Width="Auto" Binding="{Binding SelectedPerson.Weight}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

我的 ViewModel 也实现了 INotifyPropertyChanged 并且我在构造函数中创建了两个演示人员:

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace DGUpdate
{
    public class MainWindow_ViewModel : INotifyPropertyChanged
    {
        #region Properties
        public ObservableCollection<Person_Model> MyObeservableCollectionOfPerson { get; set; }
        public Person_Model SelectedPerson { get; set; }
        #endregion

        #region Constructor
        public MainWindow_ViewModel()
        {
            MyObeservableCollectionOfPerson = new ObservableCollection<Person_Model>();

            MyObeservableCollectionOfPerson.Add(
                new Person_Model()
                {
                    LastName = "Doe",
                    FirstName = "John",
                    Age = 32,
                    Weight = 90.3
                });
            MyObeservableCollectionOfPerson.Add(
                new Person_Model()
                {
                    LastName = "Miller",
                    FirstName = "Peter",
                    Age = 41,
                    Weight = 75.7
                });
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}

根据我在第一个 DataGrid 中选择的 Person,我自己的 Person-Class 具有我希望在第二个 DataGrid 中显示的四个属性:

namespace DGUpdate
{
    public class Person_Model
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public int Age { get; set; }
        public double Weight { get; set; }
    }
}

如您所见,在我的 ViewModel-Class 中,我创建了一个 Person-Object 'SelectedPerson',它绑定到我的第一个 DataGrid 的 SelectedItem 并用作我的第二个 DataGrid 的 ItemSource。我还尝试了手动调用 NotifyPropertyChanged,但不幸的是这也不起作用,因此我将它从这个 Sample-Application 中删除。

有人可以就如何解决上述问题给我建议吗?

标签: c#wpfdata-bindingdatagridselecteditem

解决方案


您必须像这样调用NotifyPropertyChanged()您的财产:

private Person_Model selectedPerson;

public Person_Model SelectedPerson { 
  get
  {
    return this.selectedPerson;
  }

  set
  {
    if (value != this.selectedPerson)
    {
      this.selectedPerson = value;
      NotifyPropertyChanged();
    }
  }
}

有关文档的更多详细信息: https ://docs.microsoft.com/fr-fr/dotnet/framework/winforms/how-to-implement-the-inotifypropertychanged-interface


推荐阅读