zoukankan      html  css  js  c++  java
  • wpf-Datagrid每行combobox设置不同值

    C# 代码

    public partial class MainWindow : Window
        {
            List<Student> students;
            public MainWindow()
            {
                InitializeComponent();
                students = new List<Student>();
                students.Add(new Student() { Id = 1, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name1" }, new select() { selectId = 2, TeamName = "Name2" } } });
                students.Add(new Student() { Id = 2, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name3" }, new select() { selectId = 2, TeamName = "Name4" } } });
                students.Add(new Student() { Id = 3, SelectedOption = 0, options = new List<select>() { new select() { selectId = 1, TeamName = "Name5" }, new select() { selectId = 2, TeamName = "Name6" } } });
    
                dg1.ItemsSource = students;
            }
            public class Student
            {
                public int Id { get; set; }
                public int SelectedOption { get; set; }
                public List<select> options { get; set; }
                public Student()
                {
                    options = new List<select>();
                }
            }
            public class Option
            {
                public List<string> myoption { get; set; }
    
                public Option()
                {
                    myoption = new List<string>();
                }
            }
            public class select
            {
                public int selectId { get; set; }
                public string TeamName { get; set; }
            }
        }

    xaml:

      <DataGrid Name="dg1" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                    <DataGridComboBoxColumn Header="Options" SelectedValueBinding="{Binding SelectedOption}" SelectedValuePath="selectId" DisplayMemberPath="TeamName">
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=options}" />
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=options}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    SelectedOption属性是默认Combobox选择第几行。


  • 相关阅读:
    Python
    TERSUS笔记116-删除数据操作
    TERSUS笔记115-修改数据操作
    TERSUS笔记114-表格分页操作
    TERSUS笔记113-查询数据操作
    TERSUS笔记111-上传图片在列表中显示
    TERSUS笔记110-增加保存验证数据操作
    TERSUS笔记103-本地开发测试mysql数据库连接
    TERSUS笔记102-CSS样式和icon图标设置说明
    TERSUS笔记101-常用元件和操作说明
  • 原文地址:https://www.cnblogs.com/dangnianxiaoqingxin/p/12910704.html
Copyright © 2011-2022 走看看