我在WPF中创建了一个小样本应用程序来演示我当前的问题: 在我的应用程序中,我有两个DataGrid。
不幸的是,我的第二个DataGrid没有刷新。在第一个DataGrid中选择一个项目时显示任何内容,尽管我用Google搜索并阅读了很多东西,但我自己却无法解决。
这就是我在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类中,我创建了一个Person-Object'SelectedPerson',该对象绑定到第一个DataGrid的SelectedItem,并用作第二个DataGrid的ItemSource。 我也手动调用了NotifyPropertyChanged,但不幸的是它也没有用,因此我将它从此示例应用程序中删除了。
有人可以给我一些有关如何解决上述问题的建议吗?
答案 0 :(得分:1)
您必须像这样在财产中致电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