• 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<--- ");
            }
        }
  • 相关阅读:
    default关键字用法
    【转载】SpringMvc和servlet简单对比介绍
    build模式入门,build模式理解(转载)
    tomcat logs 目录下各日志文件的含义
    @Component, @Repository, @Service,@Controller的区别
    在项目开发时为什么要先写接口,再写实现类?
    java 中static关键字注意事项
    this关键字使用注意事项
    两个对象之前如何建立联系
    html页面监听事件
  • 原文地址:https://www.cnblogs.com/xinzheng/p/5370869.html
Copyright © 2020-2023  润新知