C# wpf 数据网格组合框列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19003133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:52:15  来源:igfitidea点击:

wpf datagrid combobox column

c#wpfdatagridcomboboxcolumn

提问by user231605

I have trouble reading the field. I have tried in different ways but still can not. I want to read the value that the user selected the following 3 values.

我在阅读领域有困难。我尝试了不同的方法,但仍然不能。我想读取用户选择以下 3 个值的值。

Code in XAML

XAML 中的代码

<DataGridComboBoxColumn X:Name="dgcbc" Header="Wynik"/>

Code in C #

C#中的代码

List<string> list = new List <string> ();
lista.Add ("Prize");
lista.Add ("Draw");
lista.Add ("Lost");
dgcbc.ItemsSource = list;

采纳答案by RonakThakkar

This sample might help you in understanding how listbox can be used.

此示例可能会帮助您了解如何使用列表框。

public class Employee
{
    public string Name { get; set; }
    public string Gender { get; set; }        
}

XAML

XAML

<StackPanel>
  <DataGrid AutoGenerateColumns="False" Name="myGrid" Margin="10">
     <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Name}" />             
        <DataGridComboBoxColumn Width="100" x:Name="Gender" 
                    SelectedValueBinding="{Binding Gender, Mode=TwoWay}"  
                    DisplayMemberPath="{Binding Gender}" />
     </DataGrid.Columns>
  </DataGrid>
  <Button Name="ShowPersonDetails"  
          Content="Show Person Details" 
          Width="200" Height="30"  
          Click="ShowPersonDetails_Click" Margin="10" />
</StackPanel>

Code-behind

代码隐藏

public partial class WPFDataGridComboBox : Window
{
    public List<Employee> Employees { get; set; }
    public List<string> Genders { get; set; }

    public WPFDataGridComboBox()
    {
        Employees = new List<Employee>()
        {
            new Employee() { Name = "ABC", Gender = "Female" },
            new Employee() { Name = "XYZ" }
        };

        Genders = new List<string>();
        Genders.Add("Male");
        Genders.Add("Female");

        InitializeComponent();
        myGrid.ItemsSource = Employees;
        Gender.ItemsSource = Genders;
    }

    private void ShowPersonDetails_Click(object sender, RoutedEventArgs e)
    {
        foreach (Employee employee in Employees)
        {
            string text = string.Empty;
            text = "Name : " + employee.Name + Environment.NewLine;
            text += "Gender : " + employee.Gender + Environment.NewLine;
            MessageBox.Show(text);
        }
    }
}

回答by Rajiv

I guess you want to enable multi selection in the combobox inside DataGridComboBoxColumn. Following code project does the same.

我猜您想在 DataGridComboBoxColumn 内的组合框中启用多选。以下代码项目执行相同的操作。

http://www.codeproject.com/Articles/21085/CheckBox-ComboBox-Extending-the-ComboBox-Class-and

http://www.codeproject.com/Articles/21085/CheckBox-ComboBox-Extending-the-ComboBox-Class-and