将 DataGridComboBoxColumn 绑定到 class 中声明的枚举
Binding DataGridComboBoxColumn to an enum declared inside a class
我有一个绑定到我的数据库的数据网格:
我希望性别列是一个组合框,其选定值来自数据库。性别是成员内部的枚举 class:
namespace Elysium.model
{
class Member
{
public enum MemberType { Standard, Premium }
public enum Status { Inactive, Active }
public enum Gender { Male, Female }
我尝试通过以下方式绑定它,但它给了我一个错误,即命名空间中没有定义性别,或者当我这样做时不允许嵌套 <x:Type TypeName="local.Member:Gender" ></x:Type>
:
<ObjectDataProvider x:Key="genderEnum" MethodName="GetValues" ObjectType="{x:Type LocalValueEnumerator}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Gender" ></x:Type>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
ViewMembers.xaml:
<Grid DataContext="{StaticResource memberViewSource}">
<DataGrid x:Name="memberDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected" Width="1100" Height="600">
<DataGrid.Columns>
<DataGridTextColumn x:Name="memberIDColumn" Binding="{Binding MemberID}" Header="Member ID" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="fatherNameColumn" Binding="{Binding FatherName}" Header="Father Name" Width="SizeToHeader"/>
<DataGridComboBoxColumn x:Name="genderColumn" SelectedValueBinding="{Binding Value}" Header="Gender" Width="SizeToHeader"/>
ViewMembers.xaml.cs:
public partial class ViewMembers : Page
{
DataTable memberTable = new DataTable();
public ViewMembers()
{
InitializeComponent();
MemberDatabase members = MemberDatabase.getApplicationDatabase();
memberTable = members.getAllMembers();
memberDataGrid.ItemsSource = memberTable.DefaultView;
}
项目结构:
ObjectType
应设置为 System.Enum
:
<ObjectDataProvider x:Key="genderEnum"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Gender" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
用法:
<DataGridComboBoxColumn x:Name="genderColumn"
ItemsSource="{Binding Source={StaticResource genderEnum}}" ... />
我有一个绑定到我的数据库的数据网格:
namespace Elysium.model
{
class Member
{
public enum MemberType { Standard, Premium }
public enum Status { Inactive, Active }
public enum Gender { Male, Female }
我尝试通过以下方式绑定它,但它给了我一个错误,即命名空间中没有定义性别,或者当我这样做时不允许嵌套 <x:Type TypeName="local.Member:Gender" ></x:Type>
:
<ObjectDataProvider x:Key="genderEnum" MethodName="GetValues" ObjectType="{x:Type LocalValueEnumerator}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Gender" ></x:Type>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
ViewMembers.xaml:
<Grid DataContext="{StaticResource memberViewSource}">
<DataGrid x:Name="memberDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" RowDetailsVisibilityMode="VisibleWhenSelected" Width="1100" Height="600">
<DataGrid.Columns>
<DataGridTextColumn x:Name="memberIDColumn" Binding="{Binding MemberID}" Header="Member ID" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="fatherNameColumn" Binding="{Binding FatherName}" Header="Father Name" Width="SizeToHeader"/>
<DataGridComboBoxColumn x:Name="genderColumn" SelectedValueBinding="{Binding Value}" Header="Gender" Width="SizeToHeader"/>
ViewMembers.xaml.cs:
public partial class ViewMembers : Page
{
DataTable memberTable = new DataTable();
public ViewMembers()
{
InitializeComponent();
MemberDatabase members = MemberDatabase.getApplicationDatabase();
memberTable = members.getAllMembers();
memberDataGrid.ItemsSource = memberTable.DefaultView;
}
项目结构:
ObjectType
应设置为 System.Enum
:
<ObjectDataProvider x:Key="genderEnum"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Gender" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
用法:
<DataGridComboBoxColumn x:Name="genderColumn"
ItemsSource="{Binding Source={StaticResource genderEnum}}" ... />