我正在学习有关数据绑定的知识,尤其是与DataGrid
的绑定。在这里的代码中,我有一个DataGrid
和一个Label
,它们显示了DataGrid
的第一个单元格值。 XAML的输出类似于。考虑下图,第一个单元格值旁边的标签内容为: 猴子,我想我是从DataGrid第一个单元格获得的。现在,我要在第一个单元格值的左侧更新:当我在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>
感谢您的帮助。
答案 0 :(得分:1)
直接绑定到DataGrid
第一列绑定到的同一属性:
<Label Content="{Binding animelistforbinding[0].FirstName}"/>
...或在设置FirstCell
中第一项的FirstName
属性时设置animelistforbinding
属性。您可以通过处理视图模型中第一项的PropertyChanged
事件来做到这一点:
public MainWindowVM()
{
addinganime.Add(new animies("Monkey", "D Luffy"));
animelistforbinding = addinganime;
FirstCell = animelistforbinding[0].FirstName;
SecondCell = animelistforbinding[0].LastName;
animelistforbinding[0].PropertyChanged += (s, e) => FirstCell = animelistforbinding[0].FirstName;
}