c# - 基于其他 DataGrid 中的 SelectedItem 在 DataGrid 中显示对象变量
问题描述
我在 WPF 中创建了一个小型示例应用程序来演示我当前的问题: 在我的应用程序中,我有两个 DataGrid。
- 第一个 DataGrid 绑定到我自己的 Person-Objects 的 ObservableCollection,它只显示每个 Person-Object 的一个属性。
- 在第二个 DataGrid 中,我想根据我的第一个 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 中删除。
有人可以就如何解决上述问题给我建议吗?
解决方案
您必须像这样调用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
推荐阅读
- powershell - Powershell 图形用户界面。从动态列表框中选择一个项目,随后从 Active Directory 中卸载用户信息
- c# - 如何在 WinForms 按钮的文本之前添加图像图标?
- docker - Docker多阶段不调用入口点
- wordpress - 收到一定数量的视图后如何在 WordPress 上隐藏内容
- python - 使用Django、python和vue axiox提交表单后重定向到主页
- python - 我应该将数据库连接或游标传递给一个类
- mysql - 由于阻塞导致的 Laravel 连接数据库问题
- regex - 使用正则表达式排除字符组合或添加字母
- google-cloud-run - 无法使用来自另一个项目的映像部署 Cloud Run 服务
- c# - 当第二层在第一层和第三层之间时,如何避免它们之间的碰撞?Unity 2D C#