WPF数据网格组合框列 [英] wpf datagrid combobox column

查看:125
本文介绍了WPF数据网格组合框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦阅读领域。我曾尝试用不同的方式,但仍然不能。我想读用户选择以下3个值的值。



代码在XAML

 < D​​ataGridComboBoxColumn X:NAME =dgcbc标题=Wynik/> 

代码在C#



 列表与LT;字符串>名单=新名单,LT;字符串> (); 
lista.Add(奖);
lista.Add(抽奖);
lista.Add(迷失);
dgcbc.ItemsSource =清单;


解决方案

这个样本可以帮助您了解如何列表框即可。使用

 公共类Employee 
{
公共字符串名称{;组; }
公共字符串性别{搞定;组; }
}



XAML

 <&StackPanel的GT; 
<数据网格的AutoGenerateColumns =假名称=myGrid保证金=10>
< D​​ataGrid.Columns>
< D​​ataGridTextColumn绑定={绑定路径=名称}/>
< D​​ataGridComboBoxColumn WIDTH =100X:NAME =性别
SelectedValueBinding ={结合性别,模式=双向}
的DisplayMemberPath ={结合性别}/>
< /DataGrid.Columns>
< / DataGrid的>
<按钮名称=ShowPersonDetails
含量=显示个人详细资料
WIDTH =200HEIGHT =30
点击=ShowPersonDetails_Click保证金=10 />
< / StackPanel的>



代码隐藏

 公共部分类WPFDataGridComboBox:窗口
{
公开名单<员工>员工{搞定;组; }
公开名单<串GT;性别{搞定;组; }

公共WPFDataGridComboBox()
{
员工=新的List<员工>()
{
新员工(){名称=ABC ,性别=女},
新员工(){名称=XYZ}
};

性别=新的List<串GT;();
Genders.Add(男);
Genders.Add(女);

的InitializeComponent();
myGrid.ItemsSource =雇员;
Gender.ItemsSource =性别;
}

私人无效ShowPersonDetails_Click(对象发件人,RoutedEventArgs E)
{
的foreach(在员工员工员工)
{
字符串文本=的String.Empty;
文本=名称:+ employee.Name + Environment.NewLine;
文字+ =性别:+ employee.Gender + Environment.NewLine;
MessageBox.Show(文本);
}
}
}


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 XAML

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

Code in C #

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

解决方案

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);
        }
    }
}

这篇关于WPF数据网格组合框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆