wpf DataGridComboBoxColumn 绑定到 List<String>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18580036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:34:01  来源:igfitidea点击:

DataGridComboBoxColumn Binding to a List<String>

c#wpfxamldatagridcombobox

提问by mHelpMe

I have a WPF application that contains a datagrid. The datagrid is bound to my object OrderBlock which contains a List of type Orders.

我有一个包含数据网格的 WPF 应用程序。数据网格绑定到我的对象 OrderBlock,其中包含一个 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".

这工作正常,并在我的数据网格中很好地显示。我的列表中有一个属性 (StatusGood),但我想显示为一个组合框,其中只能有两个值,“发送”或“保留”。

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.

所以我试图将组合框值绑定到 List StatusList ,如下所示。然后尝试将实际值绑定到我的对象。

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

我是 WPF 的新手,必须努力理解它。我已经参考但显然没有完全理解它。http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx

Any help would be great! Thanks, M

任何帮助都会很棒!谢谢,米

回答by vitaliy zadorozhnyy

It looks like DataGridComboBoxColumn->SelectedItemBinding has to be in your case:

看起来 DataGridComboBoxColumn->SelectedItemBinding 必须在您的情况下:

 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.

并且您还必须设置 DataGridComboBoxColumn 的 ItemsSource 属性并修改您的 ViewModel 以提供组合值以使用属性(StatusList)而不是字段。

VM:

虚拟机:

public class ViewModel 
{
    public List<string> StatusList { get; set; }

    // constructor
    public ViewModel()
    {
        StatusList = new List<string>();
        StatusList.Add("Hold");
        StatusList.Add("Send");
    }

}

XAML:

XAML:

 <DataGrid.Resources>
        <local:ViewModel x:Key="ComboItems"  />
 </DataGrid.Resources>

<DataGridComboBoxColumn SelectedItemBinding="{Binding StatusGood}" ItemsSource="{Binding Path=StatusList, Source={StaticResource ComboItems}}" >

回答by BendEg

I have a solution, where your List is a ComboBoxItem, would this be possible?

我有一个解决方案,其中您的 List 是 ComboBoxItem,这可能吗?

Here is my sample XAML:

这是我的示例 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

我的 C# 类

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...

我希望这能帮助你...