首先Model层,建立实体Menu
public class Menu { public int ID { get; set; } public string ClassName { get; set; } public int ParentID { get; set; } public int OrderID { get; set; } }
然后再建立Menu的集合类Menus
public class Menus { public List<Menu> menu; public List<Menu> getMenu() { menu = new List<Menu>{ new Menu{ID=1,ClassName="菜单1",ParentID=0,OrderID=1}, new Menu{ID=2,ClassName="菜单2",ParentID=0,OrderID=2}, new Menu{ID=3,ClassName="菜单3",ParentID=0,OrderID=3} }; return menu; } }
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Human}" SelectedItem="{Binding GetOne,Mode=TwoWay}" HorizontalAlignment="Left" Name="dataGrid1" > </DataGrid> <TextBox Text="{Binding GetOne.ID,Mode=OneWay}" Height="23" HorizontalAlignment="Left" Margin="247,149,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" /> <TextBox Text="{Binding GetOne.ClassName,Mode=OneWay}" Height="23" HorizontalAlignment="Left" Margin="247,191,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
public class MenuViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public List<Menu> Human { get; set; } private Menu _getOne; public Menu GetOne { get { return _getOne; } set { _getOne = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("GetOne")); } } } public MenuViewModel() { Human = new Menus().getMenu(); } }