简体   繁体   中英

WPF DataGrid Combobox column not saving values when item is selected

I've wanted to create a ComboBoxColumn with specific options for each row

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

<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). Whenever i select something and change cell the selection is gone with no errow thrown. 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.

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:

<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; /*...*/ }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM