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

时间:2017-04-30 05:50:27

标签: wpf mvvm enums datagrid converters

我有两个转换器。一个用于显示枚举名称,另一个用于在将信息传递给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 for Resources

<UserControl.Resources>
    <converters:DetectionChemistryDisplaynameListProvider x:Key="displayEnumNameConverter" />
    <converters:DetectionChemistryConverter x:Key="enumValueConverter"/>
</UserControl.Resources>

Xmal For Combo Box with工作正常

 <ComboBox Grid.Row="2" ItemsSource="{Binding Converter={StaticResource displayEnumNameConverter}}" SelectedItem="{Binding SelectedComboDetectionChemistry, Converter={StaticResource enumValueConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Xmal for dataGrid无法正常工作

 <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。所以我不能为我的Enum Model使用Object Data Provider。请相应地建议。

1 个答案:

答案 0 :(得分:1)

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

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

id