在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中對應的單元格會自動更新。