[英]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:假设您已将DataGrid
的ItemsSource
设置或绑定到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.