wpf datagrid combobox column

后端 未结 2 772
既然无缘
既然无缘 2020-12-06 11:02

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.

Code in X

相关标签:
2条回答
  • 2020-12-06 11:46

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

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

    0 讨论(0)
  • 2020-12-06 11:57

    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

    <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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题