我正在尝试将Enum
绑定到ComboBox
。我见过很多人使用ObjectDataProvider
,但我似乎无法访问它。我还注意到有些人在Window.Resources
而不是Page.Resources
中使用它,但我无法在Page.Resources
找到它的用法。我一直在寻找解决方案几个小时。
到目前为止我所拥有的:
XAML
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sports;assembly=Sports"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ViewModel="using:Sports.ViewModel"
xmlns:model="using:Sports.Model"
xmlns:system="using:System"
x:Class="Sports.MainPage"
mc:Ignorable="d">
<Page.DataContext>
<ViewModel:CreateSubsVM/>
</Page.DataContext>
<Page.Resources>
<ObjectDataProvider></ObjectDataProvider>
</Page.Resources>
</Grid>
</Page>
C#
public enum SubsAmount
{
[Display(Description = "One Year")]
Oneyear = 0,
[Display(Description = "Two Years")]
TwoYears = 1,
[Display(Description = "Three Years")]
ThreeYears = 2
}
public class ComboboxConverter: IValueConverter
{
public string GetEnumValues(Enum enumObj)
{
DisplayAttribute attribute = enumObj.GetType().
GetRuntimeField(enumObj.ToString()).
GetCustomAttributes(typeof(SubsAmount), false).
SingleOrDefault() as DisplayAttribute;
return attribute == null ? enumObj.ToString() : attribute.Description;
}
public object Convert(object value, Type targetType, object parameter, string language)
{
return GetEnumValues((Enum) value);
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return Enum.ToObject(targetType, value);
}
}
答案 0 :(得分:0)
以下是页面对象的示例(根据MSDN documentation,对页面使用ObjectDataProvider没有任何限制):
更新#1
<强>的Xaml 强>
@MappedSuperclass
public abstract class EntityBase implements IEntity {
...
}
@MappedSuperclass
public abstract class StandardEntityJpaImpl extends EntityBase implements IStandardEntity {
...
}
@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ArtifactBase extends StandardEntityJpaImpl {
...
}
@Entity
@Table(name="artifact")
public class Artifact extends ArtifactBase implements Comparable<Artifact> {
...
}
@Entity
@Table(name="artifact2")
public class Artifact2 extends ArtifactBase implements Comparable<Artifact> {
...
}
@Entity
@Table(name="artifact3")
public class Artifact3 extends ArtifactBase implements Comparable<Artifact> {
...
}
@Entity
@Table(name="reference")
public class Reference extends StandardEntityJpaImpl implements Comparable<Artifact> {
...
}
以下是自定义数据提供者代码
[Persistence Model][1] http://i.stack.imgur.com/2uynO.png
属性代码和枚举:
<Page x:Class="PageBasedApp.MyPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:pageBasedApp="clr-namespace:PageBasedApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="MyPage">
<Page.Resources>
<ObjectDataProvider x:Key="Gestures" MethodName="GetValues" ObjectType="{x:Type ApplicationGesture}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ApplicationGesture" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider x:Key="SubAmounts" MethodName="GetShortListOfApplicationGestures" ObjectType="{x:Type pageBasedApp:DisplayAttributeBasedObjectDataProvider}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="pageBasedApp:SubsAmount" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Page.Resources>
<Grid>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
<Label Content="All Gestures:"/>
<ComboBox ItemsSource="{Binding Source={StaticResource Gestures}}" Width="150"/>
<Label Content="Sub Amounts:"/>
<ComboBox ItemsSource="{Binding Source={StaticResource SubAmounts}}" Width="150"/>
</StackPanel>
</Grid>
P.S。你在这里不需要任何转换器。 问候。