Getting selected value of ComboBox in DataGrid
How can I get the selected value of a combo box that is in a datagrid?
I've simplefied my problem so that it is easier for someone to help. Here is the 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&开发者_如何转开发gt;
</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>
Here is the code behind:
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.
}
}
}
Here is an example where I am using a telerik control but that matters not.
<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>
Notice, that I just set the selected item to a binding with an object of that type... So what this means is that I have access to that whole entire object, and I can anywhere I choose look and see where is the list it is etc... The whole point is instead of tyring to keep track of one property of the object collection, you keep track of the whole object in that collection.
Unless there is some strange nested properties in the "content" property that the ItemsSource
is bound to, I'm guessing the ComboBox
isn't even displaying data.
The Combobox
should likely be defined as:
<ComboBox
ItemsSource="{Binding content}"
DisplayMemberPath="ContentName"
SelectedValuePath="ContentID"
Name="ContentSelector" />
Where ContentName
and ContentID
are properties of content
which is defined as the ItemsSource
.
精彩评论