wpf DataGridComboBoxColumn
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication5"
xmlns:system="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Name="this" Height="556" Width="851">
<StackPanel>
<DataGrid ItemsSource="{Binding Path=SelectedList,ElementName=this}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="文本模式" Binding="{Binding Path=Value}" /> <DataGridComboBoxColumn Header="ComboBox模式" SelectedValueBinding="{Binding Path=Value}" ItemsSource="{Binding Path=SelectionList,ElementName=this}" /> <DataGridTemplateColumn Header="Template模式"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox SelectedValue="{Binding Path=Value}" ItemsSource="{Binding Path=SelectionList ,ElementName=this}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridComboBoxColumn Header="ComboBox模式(修正)"> <DataGridComboBoxColumn.EditingElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=this}" /> <Setter Property="SelectedValue" Value="{Binding Path=Value,UpdateSourceTrigger=PropertyChanged}"/> </Style> </DataGridComboBoxColumn.EditingElementStyle> <DataGridComboBoxColumn.ElementStyle> <Style TargetType="ComboBox"> <Setter Property="ItemsSource" Value="{Binding Path=SelectionList,ElementName=this}" /> <Setter Property="SelectedValue" Value="{Binding Path=Value}"/> </Style> </DataGridComboBoxColumn.ElementStyle> </DataGridComboBoxColumn> </DataGrid.Columns> </DataGrid> <Button Content="观察选中的数据" Click="Button_Click" Margin="10"/> <TextBox Name="TBX_Selected" /> </StackPanel>
</Window>
public class DataItem { public int Value { get; set; } public DataItem(int val) { Value = val; } }
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); SelectedList.Add(new DataItem(1)); SelectedList.Add(new DataItem(2)); SelectedList.Add(new DataItem(3)); SelectedList.Add(new DataItem(3)); SelectedList.Add(new DataItem(3)); SelectedList.Add(new DataItem(3)); SelectedList.Add(new DataItem(3)); // 可选数据 SelectionList.Add(1); SelectionList.Add(2); SelectionList.Add(3); } public ObservableCollection<int> SelectionList { get { return _selectionList; } set { _selectionList = value; } } private ObservableCollection<int> _selectionList = new ObservableCollection<int>(); public ObservableCollection<DataItem> SelectedList { get { return _selectedList; } set { _selectedList = value; } } private ObservableCollection<DataItem> _selectedList = new ObservableCollection<DataItem>(); private void Button_Click(object sender, RoutedEventArgs e) { TBX_Selected.Text = ""; foreach (var item in SelectedList) TBX_Selected.Text += item.Value.ToString(" --->0<--- "); } }