WPF使用DataGridComboBoxColumn完成绑定

 在使用DataGrid的时候,有时候需要使某些列为ComboBox,这时自然想到使用DataGridComboBoxColumn,但是如果使用的是ItemsSource数据绑定后台的对象,就会发现,这根本就不能用。
  首先,看有问题的代码:

复制代码
1 using System.Windows;
2 using System.Collections.ObjectModel;
3 using System.ComponentModel;
4
5 namespace WPFTest
6 {
7 ///
8 /// 数据项
9 ///
10 public class DataItem
11 {
12 public int Value { get; set; }
13 public DataItem(int val) { Value = val; }
14 }
15 ///
16 /// MainWindow.xaml 的交互逻辑
17 ///
18 public partial class MainWindow : Window
19 {
20 public MainWindow()
21 {
22 InitializeComponent();
23 // 选中数据
24 SelectedList.Add(new DataItem(1)); SelectedList.Add(new DataItem(2));
25 // 可选数据
26 SelectionList.Add(1); SelectionList.Add(2); SelectionList.Add(3);
27 }
28
29 ///
30 /// 选中的数据列表
31 ///
32 public ObservableCollection SelectedList
33 {
34 get { return _selectedList; }
35 set { _selectedList = value; }
36 }
37 private ObservableCollection _selectedList = new ObservableCollection();
38
39 ///
40 /// 可供选择的数据列表
41 ///
42 public ObservableCollection SelectionList
43 {
44 get { return _selectionList; }
45 set { _selectionList = value; }
46 }
47 private ObservableCollection _selectionList = new ObservableCollection();
48
49 // 显示选中的
50 private void Button_Click(object sender, RoutedEventArgs e)
51 {
52 TBX_Selected.Text = “”;
53 foreach (var item in SelectedList)
54 TBX_Selected.Text += item.Value.ToString(” —>0<— “);
55 }
56
57 }
58 }
复制代码
  前台为:
复制代码
1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值