【发布时间】:2017-09-27 22:32:39
【问题描述】:
我有两个转换器。一个用于显示 Enum 名称,另一个用于在将信息传递给 viewmodel 之前将所选项目值转换为 Enum 类型。
使用组合框一切正常。但同样不适用于 DataGrid 中的 DataGridComboBoxColumn。我缺少什么?
class DetectionChemistryDisplaynameListProvider : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var listOfDetectionChemistry = new List<string>();
listOfDetectionChemistry.Add(Enum.GetName(typeof(DetectionChemistryTest), DetectionChemistryTest.Hybridization));
listOfDetectionChemistry.Add(Enum.GetName(typeof(DetectionChemistryTest), DetectionChemistryTest.Hydrolysis));
listOfDetectionChemistry.Add(Enum.GetName(typeof(DetectionChemistryTest), DetectionChemistryTest.Intercalating));
listOfDetectionChemistry.Add(Enum.GetName(typeof(DetectionChemistryTest), DetectionChemistryTest.Others));
return listOfDetectionChemistry;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
第二个转换器
class DetectionChemistryConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var selectedEnumvalue = (DetectionChemistryTest)value;
var selectedEnumName = Enum.GetName(typeof(DetectionChemistryTest), selectedEnumvalue);
return selectedEnumName;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var selectedEnumName = (string)value;
var selectedEnumValue = Enum.Parse(typeof(DetectionChemistryTest), selectedEnumName);
return selectedEnumValue;
}
}
Xmal 资源
<UserControl.Resources>
<converters:DetectionChemistryDisplaynameListProvider x:Key="displayEnumNameConverter" />
<converters:DetectionChemistryConverter x:Key="enumValueConverter"/>
</UserControl.Resources>
用于组合框的 Xmal 工作正常
<ComboBox Grid.Row="2" ItemsSource="{Binding Converter={StaticResource displayEnumNameConverter}}" SelectedItem="{Binding SelectedComboDetectionChemistry, Converter={StaticResource enumValueConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
dataGrid 的 Xmal 不起作用
<DataGrid Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding ChannelCombinations}" CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Channel" Binding="{Binding ChannelCombinationName}"/>
<DataGridComboBoxColumn Header="DetectionChemistry" ItemsSource="{Binding Converter={StaticResource displayEnumNameConverter}}"
SelectedItemBinding="{Binding SelectedDetectionChemistry, Converter={StaticResource enumValueConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</DataGrid.Columns>
</DataGrid>
假设我的模型看起来像
public class ChannelCombinationTest
{
public string ChannelCombinationName { get; set; }
public DetectionChemistryTest SelectedDetectionChemistry { get; set; }
}
public enum DetectionChemistryTest
{
/// <summary>
///
/// </summary>
Hydrolysis = 0,
/// <summary>
///
/// </summary>
Hybridization = 1,
/// <summary>
///
/// </summary>
Intercalating = 2,
/// <summary>
///
/// </summary>
Others = 3,
}
而且我的 ViewModel 有 ChannelCombinationTest 的 ObservableCollection。 此集合绑定到我的数据网格。
我正在关注 MVVM。所以我不能为我的枚举模型使用对象数据提供程序。请相应地提出建议。
【问题讨论】:
-
请不要使用 cmets 将信息添加到您自己的帖子中。单击edit 并以这种方式添加您的 cmets。请做确保您提供了一个良好的minimal reproducible example,可以可靠地重现问题。这意味着确保both您没有向我们提供任何不是严格重现问题所需的代码,并且您已经向我们提供了all 重现问题所需的代码,即完整程序。以上均不满足要求。另请参阅How to Ask,包括该页面底部链接的文章。
-
好的。将尝试逐步提高质量
标签: wpf mvvm enums datagrid converters