我有一个绑定到我的数据库的数据网格: 我希望性别列是一个组合框,其选择的值来自数据库。性别是Member类中的一个枚举:
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;
}
项目结构:
答案 0 :(得分:1)
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}}" ... />