深圳商城网站设计制作,做电影网站需要多打了服务器,重庆手机网站开发,网页开发应用软件首先是绑定一个属性#xff0c;属性名称无所谓。到时候看属性设置的啥#xff0c;可能要改。
DataGrid ItemsSource{Binding Index_instance}/
然后创建INotifyPropertyChanged的类#xff0c;并把相关固定的代码粘贴上去。
然后把这个目录类建好属性名称无所谓。到时候看属性设置的啥可能要改。
DataGrid ItemsSource{Binding Index_instance}/
然后创建INotifyPropertyChanged的类并把相关固定的代码粘贴上去。
然后把这个目录类建好要用 public class Index1{public int Id { get; set; }public string Name { get; set; }public string Status { get; set; }}
用这个目录类创建属性 private ObservableCollectionIndex1 _index_instance;public ObservableCollectionIndex1 Index_instance{get { return _index_instance; }set{_index_instance value;OnPropertyChanged(nameof(Index_instance));}}
再创建构造函数搞三个实例并赋值。 public Notify(){Index_instance new ObservableCollectionIndex1{new Index1() {Id 1, NameTest 1, StatusActive },new Index1() {Id 2, NameTest 2, StatusInactive },new Index1() {Id 3, NameTest 3, StatusBad },};}
最后一步把窗口资源导向这个类的实例
DataContext new Notify(); 后台代码
主窗口后台使用DataContext new Notify();对应的类名称来引入自己定义的类里面的属性。
在VieModel中
class ViewModel:INotifyPropertyChanged
{private ObservableCollectionIndex1 _dataGridSource;public ObservableCollectionIndex1 DataGridSource{get { return _dataGridSource; }set { _dataGridSource value;OnPropertyChanged(nameof(DataGridSource));}}public ViewModel(){_dataGridSource new ObservableCollectionIndex1{new Index1(){Id1,NameAlic,DescriptionActive}, new Index1(){Id2,NameBaliy,DescriptionInactive},new Index1(){Id3,NameCecy,DescriptionDown},};}//固定部分public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged(string propertyName){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
public class Index1
{public int Id { get; set; }public string Name { get; set; }public string Description { get; set; }
}
XAML部分
Window x:ClassDataGrid练习2.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:DataGrid练习2mc:IgnorabledTitleMainWindow Height450 Width800GridDataGrid Margin30 ItemsSource{Binding DataGridSource}//Grid
/Window