在WPF中,可以通过以下步骤实现GridView的数据绑定:
public class MyData : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
OnPropertyChanged(nameof(Name));
}
}
}
// 其他属性...
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<!-- 其他列... -->
</GridView>
</ListView.View>
</ListView>
public ObservableCollection<MyData> MyDataCollection { get; } = new ObservableCollection<MyData>();
// 添加数据
MyDataCollection.Add(new MyData { Name = "John" });
MyDataCollection.Add(new MyData { Name = "Alice" });
// 设置ItemSource
myListView.ItemsSource = MyDataCollection;
这样,当你修改MyData对象的属性值时,GridView中对应的单元格会自动更新。