在DataGrid中获取ComboBox的选定值

时间:2011-03-29 16:57:31

标签: c# wpf xaml datagrid combobox

如何获取数据网格中组合框的选定值?

我已经简化了我的问题,因此有人帮助更容易。这是XAML:

<Window x:Class="del_WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid DataContext="{Binding}" AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="60,32,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="368">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding test}"></DataGridTextColumn>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding combo}" DisplayMemberPath="value" SelectedValuePath="key" SelectedItem="{Binding selected, Mode=TwoWay}"></ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="353,238,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace del_WpfApplication3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            load();
        }

        public class tmp1
        {
            public string test { get; set; }
            public IList<tmp2> combo { get; set; }
            public string selected { get; set; }
        }

        public class tmp2
        {
            public string key { get; set; }
            public string value { get; set; }
            public string selected { get; set; }
        }

        private void load()
        {
            IList<tmp2> list = new List<tmp2>();
            list.Add(new tmp2(){ key = "key1", value = "value1" });
            list.Add(new tmp2(){ key = "key2", value = "value2" });

            IList<tmp1> list2 = new List<tmp1>();
            list2.Add(new tmp1() { test = "test1", combo = list });
            list2.Add(new tmp1() { test = "test2", combo = list });

            dataGrid1.ItemsSource = list2;


        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            tmp1 t = (tmp1)dataGrid1.Items[0];
            Console.WriteLine(t.selected); // this is empty when I'm expecting it to be the selected value as set in the XAML.
        }
    }
}

2 个答案:

答案 0 :(得分:0)

以下是我使用telerik控件的示例,但不重要。

 <telerik:RadComboBox Margin="5" 
        Height="28" Width="220" 
        VerticalAlignment="Top" HorizontalAlignment="Left"
        ItemsSource="{Binding Path=ProductTypeList}"
        SelectedItem="{Binding Path=SelectedProductType, Mode=TwoWay}"
        DisplayMemberPath="Code">
  </telerik:RadComboBox>

注意,我只是将所选项目设置为与该类型对象的绑定...所以这意味着我可以访问整个对象,我可以在任何地方查看,看看哪里是它是等等的列表...整点而不是跟踪以跟踪对象集合的一个属性,你跟踪该集合中的整个对象。

答案 1 :(得分:0)

除非ItemsSource绑定的“content”属性中存在一些奇怪的嵌套属性,否则我猜测ComboBox甚至没有显示数据。

Combobox应该定义为:

<ComboBox 
  ItemsSource="{Binding content}" 
  DisplayMemberPath="ContentName" 
  SelectedValuePath="ContentID" 
  Name="ContentSelector" />

其中ContentNameContentIDcontent的属性,定义为ItemsSource