[英]How to set string property value to the ComboBox control inside Datagrid?
I have a DataGrid
in my WPF project.我的 WPF 项目中有一个
DataGrid
。 The model which I am binding has property DataType
of type string
.我绑定的模型具有
string
DataType
的属性DataType
。 In normal display mode I am displaying it like:在正常显示模式下,我将其显示为:
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource CellBlock}" Text="{Binding DataType}" ToolTip="{Binding DataType}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
In edit mode I need to populate an enum
in the ComboBox
and set the selected combo box item to the item which is coming from string
model property Datatype
and if user changes the value in combobox it should get reflected in model.在编辑模式下,我需要在
ComboBox
填充一个enum
并将选定的组合框项设置为来自string
模型属性Datatype
,如果用户更改组合框中的值,它应该反映在模型中。 How can I achieve these things?我怎样才能实现这些目标?
You do not need a custom cell template for that, DataGrid
has a built-in DataGridComboBoxColumn
.您不需要自定义单元格模板,
DataGrid
有一个内置的DataGridComboBoxColumn
。 In normal mode, it displays the value as text and in edit-mode it shows a ComboBox
.在正常模式下,它将值显示为文本,在编辑模式下它显示一个
ComboBox
。
You have to expose the constants that your enum
defines as a collection.您必须公开
enum
定义为集合的常量。 You can create a simple MarkupExtension
for that that returns the values of any given enum
type.您可以为其创建一个简单的
MarkupExtension
,以返回任何给定enum
类型的值。
public class EnumValuesExtension : MarkupExtension
{
public Type Type { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
return Enum.GetValues(typeof(MyEnum));
}
}
This is an example of your DataGrid
.这是您的
DataGrid
一个示例。 As you can see, you can simply use the markup extension to bind the ItemsSource
of the combo box column to your enum
type, which is MyEnum
here.如您所见,您可以简单地使用标记扩展将组合框列的
ItemsSource
绑定到您的enum
类型,这里是MyEnum
。
<DataGrid ItemsSource="{Binding newItems}">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{local:EnumValues Type={x:Type local:MyEnum}}"
SelectedItemBinding="{Binding DataType}"/>
</DataGrid.Columns>
</DataGrid>
In your model, use the enum
type for the DataType
property.在您的模型中,对
DataType
属性使用enum
类型。 I do not think there is any use in exposing it as string
.我认为将其作为
string
公开没有任何用处。 The example above assumes that you use your enum
type, not string
.上面的示例假设您使用的是
enum
类型,而不是string
。
However, if you really depend on it being a string
, you can create a simple value converter .然而,如果你真的依赖它是一个
string
,你可以创建一个简单的值转换器。
public class StringToEnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Enum.Parse((Type)parameter, (string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}
}
This would change the binding on the combo box column to this:这会将组合框列上的绑定更改为:
<DataGridComboBoxColumn ItemsSource="{local:EnumValues Type={x:Type local:MyEnum}}"
SelectedItemBinding="{Binding DataType, Converter={StaticResource StringToEnumConverter}, ConverterParameter={x:Type local:MyEnum}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.