2013-09-25 127 views
7

我無法閱讀該字段。我嘗試過不同的方式,但仍然不能。我想讀取用戶選擇以下3個值的值。wpf datagrid combobox列

代碼在XAML

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

代碼在C#

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

你是什麼意思「我想讀值用戶選擇以下3個值。」是什麼意思?你的問題到底是什麼? – JFTxJ

+0

不要這樣做。在WPF嘗試使用綁定,它更容易:http://www.wpftutorial.net/DataGrid.html –

回答

17

此示例可以幫助您瞭解列表框中如何使用。

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> 

代碼隱藏

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

我會改變'myGrid.ItemsSource = Employees;'和'Gender.ItemsSource = Genders'來代替綁定。 –

+0

是否爲你工作? – RonakThakkar

+0

是的,我明白它是如何工作的,並且我編寫了其餘的代碼。一切正常。謝謝你和你的幫助RonakThakkar。 :) – user231605