绑定到List< String>的DataGridComboBoxColumnColumn。 [英] DataGridComboBoxColumn Binding to a List<String>
问题描述
我有一个包含数据网格的WPF应用程序。数据网格绑定到我的对象OrderBlock,其中包含Orders类型的列表。
I have a WPF application that contains a datagrid. The datagrid is bound to my object OrderBlock which contains a List of type Orders.
<DataGrid DataContext="{Binding OrderBlock}"
Name="dataGridOrdersGood"
ItemsSource="{Binding Orders}"
这工作正常,并在我的datagrid中很好地显示。我的列表中有一个属性(StatusGood),但我想显示为组合框,其中只能有两个值 Send或 Hold。
This works fine and displays nicely in my datagrid. There is one property (StatusGood) in my List though that I would like to display as a combobox where there can be only two values, "Send" or "Hold".
因此,我试图将组合框值绑定到List StatusList,如下所示。然后尝试将实际值绑定到我的对象。
So I was trying to bind the combobox values to the List StatusList as shown below. Then trying to bind the actual value to my object.
public class ViewModel : INotifyPropertyChanged
{
public List<string> StatusList;
// constructor
public ViewModel()
{
StatusList = new List<string>();
StatusList.Add("Hold");
StatusList.Add("Send");
}
}
<DataGridComboBoxColumn Header="Status Good" SelectedItemBinding="{Binding StatusList}" SelectedValuePath="{Binding StatusGood}"/>
但是,除了空的组合框以外,什么都没有显示。我不明白为什么我的组合框至少没有显示对象的价值?我提供的是列表,因此我再次不明白为什么它没有显示任何内容。
However nothing is displayed other than a empty combobox. I do not understand why at the very least my combobox is not showing the value of my object? I am providing a list so again I do not understand why it's not showing anything.
我是WPF的新手,必须努力理解它。我已经引用了,但显然没有完全理解。 http://msdn.microsoft.com/en-us /library/system.windows.controls.datagridcomboboxcolumn.aspx
I'm new to WPF and must struggling to understand it. I have referenced but obviously not fully understand it. http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx
任何帮助都会很棒!
谢谢,
M
Any help would be great! Thanks, M
推荐答案
看起来DataGridComboBoxColumn-> SelectedItemBinding必须在您的情况下:
It looks like DataGridComboBoxColumn->SelectedItemBinding has to be in your case:
SelectedItemBinding="{Binding StatusGood}"
,还必须设置DataGridComboBoxColumn的ItemsSource属性,并修改ViewModel以提供组合值以使用property(StatusList)代替字段。
and you have to set also ItemsSource property of the DataGridComboBoxColumn and modify your ViewModel for providing combo values to use property(StatusList) instead of field.
VM:
public class ViewModel
{
public List<string> StatusList { get; set; }
// constructor
public ViewModel()
{
StatusList = new List<string>();
StatusList.Add("Hold");
StatusList.Add("Send");
}
}
XAML:
<DataGrid.Resources>
<local:ViewModel x:Key="ComboItems" />
</DataGrid.Resources>
<DataGridComboBoxColumn SelectedItemBinding="{Binding StatusGood}" ItemsSource="{Binding Path=StatusList, Source={StaticResource ComboItems}}" >
这篇关于绑定到List< String>的DataGridComboBoxColumnColumn。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!