DataGridComboBoxColumn 初始值不显示
DataGridComboBoxColumn initial value not displaying
我有一个绑定到枚举的 DataGridComboBoxColumn。下拉列表正确显示枚举的值,但组合框最初不显示任何值。我正在尝试将所选值绑定到作为我的 DataGrid 源的 DataTable 的性别 属性。我已经在 SelectedItemBinding="{Binding Gender}"
或 SelectedValueBinding="{Binding Gender}"
等类似问题中尝试过这些方法,但它们对我不起作用。
我也曾尝试在 DataGridTemplateColumn 中放置一个组合框,但它最初不显示性别值我已尝试 SelectedValue="{Binding Gender}"
和 SelectedValuePath="{Binding 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;
}
您可以通过创建一个非常简单的转换器 class 来完成这项工作,该转换器从 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>
我有一个绑定到枚举的 DataGridComboBoxColumn。下拉列表正确显示枚举的值,但组合框最初不显示任何值。我正在尝试将所选值绑定到作为我的 DataGrid 源的 DataTable 的性别 属性。我已经在 SelectedItemBinding="{Binding Gender}"
或 SelectedValueBinding="{Binding Gender}"
等类似问题中尝试过这些方法,但它们对我不起作用。
我也曾尝试在 DataGridTemplateColumn 中放置一个组合框,但它最初不显示性别值我已尝试 SelectedValue="{Binding Gender}"
和 SelectedValuePath="{Binding 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;
}
您可以通过创建一个非常简单的转换器 class 来完成这项工作,该转换器从 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>