获取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.
}
}
}
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>
</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.
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个我使用 Telerik 控件的示例,但这并不重要。
请注意,我只是将所选项目设置为与该类型的对象的绑定...所以这意味着我可以访问整个对象,并且我可以在我选择的任何地方查看并查看列表在哪里等等...重点在于,您不必跟踪对象集合的一个属性,而是跟踪该集合中的整个对象。
Here is an example where I am using a telerik control but that matters not.
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.
除非
ItemsSource
绑定到的“content”属性中有一些奇怪的嵌套属性,否则我猜测ComboBox
甚至不显示数据。Combobox
应该定义为:其中
ContentName
和ContentID
是content
的属性,它被定义为 <代码>ItemsSource。Unless there is some strange nested properties in the "content" property that the
ItemsSource
is bound to, I'm guessing theComboBox
isn't even displaying data.The
Combobox
should likely be defined as:Where
ContentName
andContentID
are properties ofcontent
which is defined as theItemsSource
.