简体   繁体   English

WPF DataGrid Combobox 列在选择项目时不保存值

[英]WPF DataGrid Combobox column not saving values when item is selected

I've wanted to create a ComboBoxColumn with specific options for each row我想为每一行创建一个带有特定选项的 ComboBoxColumn

eg例如

List string列表字符串

        public ObservableCollection<string> FilterTypes { get; set; }

Lists itself列出自己

public static class FilterCharacters
    {
        public static Dictionary<FilterTypes, string> FilterCharactersList => new Dictionary<FilterTypes, string>()
                {
                    {FilterTypes.Equals, "=" },
                    {FilterTypes.NotEquals, "!=" },
                    {FilterTypes.Greater, ">" },
                    {FilterTypes.GreaterOrEqual, ">=" },
                    {FilterTypes.Less, "<" },
                    {FilterTypes.LessOrEqual, "<=" },
                    {FilterTypes.Contains, "contains" },
                    {FilterTypes.NotContains, "does not contain" },
                    {FilterTypes.True, "True" },
                    {FilterTypes.False, "False" }
                };

        public static List<string> NumberList => new List<string>()
        {
            FilterCharactersList[FilterTypes.Greater],
            FilterCharactersList[FilterTypes.GreaterOrEqual],
            FilterCharactersList[FilterTypes.Less],
            FilterCharactersList[FilterTypes.LessOrEqual],
            FilterCharactersList[FilterTypes.Equals],
            FilterCharactersList[FilterTypes.NotEquals]
        };

        public static List<string> TextList => new List<string>()
        {
            FilterCharactersList[FilterTypes.Contains],
            FilterCharactersList[FilterTypes.NotContains],
            FilterCharactersList[FilterTypes.Equals],
            FilterCharactersList[FilterTypes.NotEquals]
        };

        public static List<string> BoolList => new List<string>()
        {
            FilterCharactersList[FilterTypes.True],
            FilterCharactersList[FilterTypes.False]
        };
    }

and xaml和 xaml

<DataGridComboBoxColumn Width="120" Header="Check">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Path=FilterTypes}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding Path=FilterTypes}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

Using answer of my previous question i managed to display those items in the combo box but then i can not select other than the first one (which is empty).使用我上一个问题的答案,我设法在组合框中显示这些项目,但是除了第一个(空的)之外,我不能 select 。 Whenever i select something and change cell the selection is gone with no errow thrown.每当我 select 某些东西并更改单元格时,选择就消失了,没有抛出任何错误。 I've tried using SelectedItemBidning but it resulted in an conversion error from string to ObservableCollection, when removed the selecteditembinding there is no error but nothing happens.我尝试使用SelectedItemBidning ,但它导致从字符串到 ObservableCollection 的转换错误,当删除 selecteditembinding 时没有错误但没有任何反应。

Assuming you have set or bound the ItemsSource of the DataGrid to an IEnumerable<T> , you should set the SelectedItemBinding property to a binding to a string property of the type T that holds the currently selected value:假设您已将DataGridItemsSource设置或绑定到IEnumerable<T> ,则应将SelectedItemBinding属性设置为与包含当前选定值的T类型的字符串属性的绑定:

<DataGridComboBoxColumn Width="120" Header="Check"
                        SelectedItemBinding="{Binding SelectedFilerType}">
...

public class Item
{
    public ObservableCollection<string> FilterTypes { get; set; }
    ...

    private string _selectedFilerType;
    public string SelectedFilerType
    {
        get { return _selectedFilerType; }
        set { _selectedFilerType = value; /*...*/ }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将DataGridHeader的组合框中的Selected Item的值绑定到一个特定的列而不绑定到wpf中的所有Datagrid列? - How to bind the value of Selected Item in a combobox in DataGridHeader to one specific column not to all Datagrid columns in wpf? 当组合框本身是数据网格列的一部分时,无法将组合框选定的项目绑定到文本框 - Unable to Bind a combobox selected item to a TextBox when combobox itself is a part of a datagrid column 如何将列添加到 WPF 中所选项目的数据网格? - How to add a column to a datagrid for a selected item in WPF? wpf datagrid组合框列 - wpf datagrid combobox column WPF 中 Datagrid 内的组合框选定项绑定不起作用 - Combobox selected item binding inside Datagrid in WPF not working MVVM WPF Datagrid TemplateColumn组合框所选项目不起作用 - MVVM WPF Datagrid TemplateColumn Combobox Selected Item Not Working 组合框选择的项目WPF - Combobox selected item WPF WPF Datagrid - 在DataGrid中单击空格时取消选择所选项 - WPF Datagrid - deselect selected item(s) when clicking whitespace in the DataGrid 为数据网格中的组合框设置所选项目 - Set the selected item for a combobox in a datagrid 如何将带有值转换器的WPF组合框所选项目绑定到DataGridTextColumn? DataGridTextColumn和combobox都是datagrid列 - How to bind WPF combobox selected item with a Value Converter to a DataGridTextColumn? Both DataGridTextColumn and combobox are datagrid columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM