带枚举转换器的 DataGridComboBoxColumn 未按预期工作

DataGridComboBoxColumn with Enum Converters is not working as expected

我有两个 converters.One 用于显示枚举名称,另一个用于在将信息传递给 viewmodel 之前将所选项目值转换为类型枚举。

使用组合 box.But 一切正常,同样不适用于 DataGrid.What 中的 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 For Combo Box 工作正常

 <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。所以我不能为我的枚举使用对象数据提供程序 Model.Please 相应地建议。

这里的根本问题是 WPF 以神秘的方式运行它的奇迹。

组合框列表未出现的原因是 WPF 需要项目源的管理框架元素。你这样修...

    <FrameworkElement x:Name="dummyElement" Visibility="Collapsed"/>
    <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},Source={x:Reference dummyElement} }" 
                                    SelectedValueBinding="{Binding SelectedDetectionChemistry, Converter={StaticResource enumValueConverter}}"
                                />
            <DataGridTextColumn Header="DetectionChemistry" Binding="{Binding SelectedDetectionChemistry, Converter={StaticResource enumValueConverter}}"/>
        </DataGrid.Columns>
    </DataGrid>