[英]Getting selected value of ComboBox in DataGrid
如何获取数据网格中组合框的选定值?
我简化了我的问题,以便有人帮助。 这是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.
}
}
}
这是我使用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>
请注意,我只是将所选项目设置为具有该类型对象的绑定...因此,这意味着我可以访问整个对象,并且我可以在任何地方选择查看并查看列表的位置。等等...重点不是要跟踪对象集合的一个属性,而要跟踪该集合中的整个对象。
除非ItemsSource
绑定到“ content”属性中有一些奇怪的嵌套属性,否则我猜ComboBox
甚至不显示数据。
组合Combobox
可能应定义为:
<ComboBox
ItemsSource="{Binding content}"
DisplayMemberPath="ContentName"
SelectedValuePath="ContentID"
Name="ContentSelector" />
其中ContentName
和ContentID
是定义为ItemsSource
的content
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.