DataGridComboBoxColumn初始值未显示

时间:2019-07-18 09:22:19

标签: c# wpf datagrid datagridcomboboxcolumn

我有一个绑定到枚举的DataGridComboBoxColumn。下拉列表会正确显示enum的值,但组合框最初不会显示任何值。我试图将所选值绑定到DataTable的Gender属性,该属性是我的DataGrid的来源。我在类似SelectedItemBinding="{Binding Gender}"SelectedValueBinding="{Binding Gender}"的类似问题中尝试了这些方法,但它们对我不起作用。

我也尝试过将组合框放在DataGridTemplateColumn内,但最初我尝试SelectedValue="{Binding Gender}"SelectedValuePath="{Binding Gender}"却没有显示Gender值,但无济于事。 SelectedIndex = "0"有用,但是对我来说毫无用处,因为我希望初始值来自某个属性。

当我将属性绑定到DataGridTextColumn时,绑定将起作用,因为它显示正确的值,即male / female。

XAML:

<ObjectDataProvider x:Key="genderEnum" xmlns:sys="clr-namespace:System;assembly=mscorlib" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
    <ObjectDataProvider.MethodParameters>
        <x:Type TypeName="enumLoc:Gender"/>
    </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<Grid DataContext="ViewMembers.xaml.cs">
    <DataGrid x:Name="memberDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Path=memberTable, Mode=TwoWay}" RowDetailsVisibilityMode="VisibleWhenSelected" Width="1200" Height="600" GridLinesVisibility="None" >
        <DataGrid.Columns>                
            <DataGridComboBoxColumn x:Name="memberGenderColumn" SelectedItemBinding="{Binding Gender}" ItemsSource="{Binding Source={StaticResource genderEnum}}"  Header="Gender" Width="60">
            <DataGridTemplateColumn Header="Gender">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Source={StaticResource genderEnum}}" SelectedValue="{Binding Gender}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

XAML.cs:

    DataTable memberTable = new DataTable();
    MemberDatabase members = MemberDatabase.GetApplicationDatabase();

    public ViewMembers()
    {
        InitializeComponent();
        memberTable = members.GetAllMembers();
        memberTable.AsEnumerable().ToList().ForEach(row =>
        {
            var cellList = row.ItemArray.ToList();
            row.ItemArray = cellList.Select(x => x.ToString().Trim()).ToArray();
        });
        this.DataContext = memberTable;
        memberDataGrid.ItemsSource = memberTable.DefaultView;
    }

1 个答案:

答案 0 :(得分:2)

您可以通过创建一个非常简单的转换器类(从int转换为枚举)来实现此目的:

public class EnumConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (Gender)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value;
    }
}

XAML:

<DataGrid x:Name="memberDataGrid" ...>
    <DataGrid.Resources>
        <local:EnumConverter x:Key="EnumConverter" />
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridComboBoxColumn x:Name="memberGenderColumn" 
                                SelectedItemBinding="{Binding Gender, Converter={StaticResource EnumConverter}}" 
                                ItemsSource="{Binding Source={StaticResource genderEnum}}" Header="Gender" Width="60">
        </DataGridComboBoxColumn>
    </DataGrid.Columns>
</DataGrid>