【发布时间】:2019-04-23 00:03:36
【问题描述】:
我正在学习数据绑定,尤其是DataGrid。在我的代码中,我有一个DataGrid 和一个Label,它显示了DataGrid 的第一个单元格值。 XAML 的输出类似于。考虑下图,第一个单元格值旁边的标签内容是: Monkey,我想我是从 DataGrid 第一个单元格中得到的。现在我想要更新 The First Cell Value is: 的左侧,当我更改 DataGrid 第一个单元格中的值时。但我无法实现它。
下面是我的代码和 XAML 文件
代码
namespace DataGridExampleSelfTry
{
public class MainWindowVM:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
private string _FirstCell;
public string FirstCell
{
get{ return _FirstCell; }
set
{
_FirstCell = value;
PropertyChanged(this,new PropertyChangedEventArgs(nameof(FirstCell)));
}
}
public string SecondCell { get; set; }
private ObservableCollection<animies> _animelistforbinding;
public ObservableCollection<animies> animelistforbinding
{ get
{
return _animelistforbinding;
}
set
{
_animelistforbinding = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(animelistforbinding)));
}
}
ObservableCollection<animies> addinganime = new ObservableCollection<animies>();
public MainWindowVM()
{
addinganime.Add(new animies("Monkey", "D Luffy"));
animelistforbinding = addinganime;
FirstCell = animelistforbinding[0].FirstName;
SecondCell = animelistforbinding[0].LastName;
}
}
public class animies:INotifyPropertyChanged
{
private string _FirstName;
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
PropertyChanged(this, new PropertyChangedEventArgs(nameof(FirstName)));
}
}
public string LastName { get; set; }
public animies(string dFirstName, string dLastName)
{
FirstName = dFirstName;
LastName = dLastName;
}
}
}
XAML
<Window x:Class="DataGridExampleSelfTry.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:DataGridExampleSelfTry"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="450">
<Window.DataContext>
<local:MainWindowVM/>
</Window.DataContext>
<StackPanel>
<DataGrid x:Name="XAML_DataGrid"
AutoGenerateColumns="False" CanUserAddRows="False"
ItemsSource="{Binding animelistforbinding}" Margin="5"
CanUserSortColumns="False" HorizontalGridLinesBrush="Gray"
VerticalGridLinesBrush="Gray" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Header="First name" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" Width="*" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal">
<Label Content="The First Cell Value is : "/>
<Label Content="{ Binding FirstCell}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="The Second Cell Value is : "/>
<Label Content="{ Binding SecondCell}"/>
</StackPanel>
<Button Content="Button" Margin="50"/>
</StackPanel>
</Window>
感谢您的帮助。
【问题讨论】:
标签: wpf binding datagrid wpfdatagrid inotifypropertychanged