Wpf ComboBox 绑定关联对象列表 与实体对象中的相关ID列绑定的方式

时间:2020-10-18 09:27:29   收藏:0   阅读:32

1.ViewModel对象

public class DocumentAddUpdateViewModel : INotifyPropertyChanged
{

  public event PropertyChangedEventHandler PropertyChanged;

  public ObservableCollection<Department> AllDepartments { get; set; }

  public Person Person{get;set;}

}

public class Person{

  public String DepartmentId{get;set;}

}

在界面的类中

private DocumentAddUpdateViewModel  viewModel = new DocumentAddUpdateViewModel ();

viewModel.AllDepartments = departmentService.GetAll();

最关键的是xaml中的绑定

<ComboBox
  SelectedValue="{Binding Path=Person.DepartmentId}" /*1.这个用来绑定到对象的字段*/
  ItemsSource="{Binding Path=AllDepartments}"  /*2.用来绑定列表*/
  DisplayMemberPath="ShowName" /*3.绑定时用来显示的列*/
  SelectedValuePath="Id" /*4.这个是AllDepartments里的id,与3是一对*/
Width="250"
Height="30"
Style="{DynamicResource FormComboBoxStyle}" />

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!