问题
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}"
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".
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.
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
Any help would be great! Thanks, M
回答1:
It looks like DataGridComboBoxColumn->SelectedItemBinding has to be in your case:
SelectedItemBinding="{Binding StatusGood}"
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}}" >
回答2:
I have a solution, where your List is a ComboBoxItem, would this be possible?
Here is my sample XAML:
<DataGrid AutoGenerateColumns="False" Name="myGridTest">
<DataGrid.Columns>
<DataGridTextColumn Header="Text" Binding="{Binding MyText}" />
<DataGridTemplateColumn Header="Combobox">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedIndex="0" ItemsSource="{Binding ComboList}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
My C#-Class
public class Test
{
private string _MyText;
private IList<ComboBoxItem> _ComboList;
public Test()
{
_MyText = "Test 123";
_ComboList = new List<ComboBoxItem>();
_ComboList.Add(new ComboBoxItem() { Content = "Next", IsSelected = true });
_ComboList.Add(new ComboBoxItem() { Content = "Prev." });
}
public IList<ComboBoxItem> ComboList
{
get { return _ComboList; }
set { _ComboList = value; }
}
public string MyText
{
get { return _MyText; }
set { _MyText = value; }
}
}
And for Testing:
List<Test> cList = new List<Test>();
cList.Add(new Test());
cList.Add(new Test());
cList.Add(new Test());
cList.Add(new Test());
cList.Add(new Test());
myGridTest.ItemsSource = cList;
I hope this help you...
来源:https://stackoverflow.com/questions/18580036/datagridcomboboxcolumn-binding-to-a-liststring