WPF DataGridComboxColumn 数据源绑定
采用DataGridComboxColumn模板和在 DataGridTemplateColumn模板中添加Combox实现数据绑定
图一Combox 采用tb数据源字段Selection绑定
图二 采用 依赖属性绑定DataGridComboxColums
Back Code
1 DataTable tb; 2 3 /// <summary> 4 /// 下拉框数据源 5 /// </summary> 6 public ObservableCollection<int> SelectionList 7 { 8 get { return _selectionList; } 9 set { _selectionList = value; } 10 } 11 private ObservableCollection<int> _selectionList = new ObservableCollection<int>(); 12 13 public 窗体() 14 { 15 InitializeComponent(); 16 17 SelectionList.Add(1); 18 SelectionList.Add(2); 19 SelectionList.Add(3); 20 21 Banding(); 22 23 24 } 25 26 27 private void Banding() 28 { 29 tb = new DataTable(); 30 DataColumn Checked = new DataColumn("Checked"); 31 DataColumn Name = new DataColumn("Name"); 32 DataColumn Sex = new DataColumn("Sex"); 33 DataColumn Age = new DataColumn("Age"); 34 DataColumn Selection = new DataColumn("Selection", typeof(ObservableCollection<int>)); 35 tb.Columns.Add(Checked); 36 tb.Columns.Add(Name); 37 tb.Columns.Add(Sex); 38 tb.Columns.Add(Age); 39 tb.Columns.Add(Selection); //如果绑定数据不采用依赖属性绑定 采用数据模板添加 Combox 则该下拉框的数据源为Selection 40 for (int i = 0; i < 5; i++) 41 { 42 DataRow row = tb.NewRow(); 43 row["Name"] = "AA" + i.ToString(); 44 if (i % 2 == 0) 45 { 46 row["Checked"] = true; 47 row["Sex"] = "男"; 48 } 49 else 50 { 51 row["Checked"] = false; 52 row["Sex"] = "女"; 53 } 54 row["Age"] = i.ToString(); 55 row["Selection"] = SelectionList; 56 tb.Rows.Add(row); 57 } 58 //如果数据源绑定要用DataContent上下文数据绑定,则xmal界面必须指定ItemSource="{Binding}" 59 dg_Grid.DataContext = tb; 60 61 }
XMAL 代码
1 <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" CanUserAddRows="False" Name="dg_Grid" AreRowDetailsFrozen="False" AllowDrop="True" Margin="23,0,23,32"> 2 <DataGrid.Columns> 3 <DataGridCheckBoxColumn Header="编号" Binding="{Binding Checked}"/> 4 <DataGridTemplateColumn Header="姓 名" Width="100"> 5 <DataGridTemplateColumn.CellTemplate> 6 <DataTemplate> 7 <TextBox Text="{Binding Name}"/> 8 </DataTemplate> 9 </DataGridTemplateColumn.CellTemplate> 10 </DataGridTemplateColumn> 11 <DataGridTemplateColumn Header="年 龄"> 12 <DataGridTemplateColumn.CellTemplate > 13 <DataTemplate > 14 <TextBox Text="{Binding Age}"/> 15 </DataTemplate> 16 </DataGridTemplateColumn.CellTemplate> 17 </DataGridTemplateColumn> 18 <!--不使用依赖属性绑定下拉框时,数据源中必须包含该属性字段--> 19 <DataGridTemplateColumn Header="Template模式"> 20 <DataGridTemplateColumn.CellTemplate> 21 <DataTemplate> 22 <ComboBox SelectedValue="{Binding Path=Value}" ItemsSource="{Binding Path=Selection}" /> 23 </DataTemplate> 24 </DataGridTemplateColumn.CellTemplate> 25 </DataGridTemplateColumn> 26 27 <!--依赖属性绑定数据源下拉框必须同时指定EditingElementStyle、ElemengStyle才能显示下拉框中的数据--> 28 <DataGridComboBoxColumn Header="ComboBox模式(修正)"> 29 <DataGridComboBoxColumn.EditingElementStyle> 30 <Style TargetType="ComboBox"> 31 <Setter Property="ItemsSource" Value="{Binding Path=SelectionList,RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}}" /> 32 <Setter Property="SelectedValue" Value="{Binding Path=Value}" /> 33 </Style> 34 </DataGridComboBoxColumn.EditingElementStyle> 35 <DataGridComboBoxColumn.ElementStyle> 36 <Style TargetType="ComboBox"> 37 <Setter Property="ItemsSource" Value="{Binding Path=SelectionList,RelativeSource={RelativeSource AncestorType={x:Type Window},Mode=FindAncestor}}" /> 38 <Setter Property="SelectedValue" Value="{Binding Path=Value}" /> 39 </Style> 40 </DataGridComboBoxColumn.ElementStyle> 41 </DataGridComboBoxColumn> 42 <DataGridTemplateColumn Header="图 片"> 43 <DataGridTemplateColumn.CellTemplate> 44 <DataTemplate > 45 <Image Source="{Binding Path=Address}"/> 46 </DataTemplate> 47 </DataGridTemplateColumn.CellTemplate> 48 </DataGridTemplateColumn> 49 </DataGrid.Columns> 50 </DataGrid>