首页 > Windows开发 > 详细

wpf-Datagrid每行combobox设置不同值

时间:2020-05-18 15:36:23      阅读:92      评论:0      收藏:0      [点我收藏+]

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选择第几行。


wpf-Datagrid每行combobox设置不同值

原文:https://www.cnblogs.com/dangnianxiaoqingxin/p/12910704.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!