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<--- ");
        }
    }

 

转载于:https://www.cnblogs.com/xinzheng/p/5370869.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值