C# 组合框中的Caliburn.Micro枚举绑定

C# 组合框中的Caliburn.Micro枚举绑定,c#,wpf,enums,combobox,caliburn.micro,C#,Wpf,Enums,Combobox,Caliburn.micro,我有一个枚举 public enum FuelType { Diesel, Petrol, E10 } 如何将其绑定到Caliburn.Micro的组合框 xaml: 以及ModelView中的属性: public FuelType Fuel { get { return _fuel; } set { _fuel = value; Notify

我有一个枚举

    public enum FuelType
{
    Diesel,
    Petrol,
    E10
}
如何将其绑定到Caliburn.Micro的组合框

xaml:

以及ModelView中的属性:

        public FuelType Fuel
    {
        get { return _fuel; }
        set
        {
            _fuel = value;
            NotifyOfPropertyChange(nameof(Fuel));
        }
    }

正确的方法是在ViewModel中有一个项目列表和所选项目。Caliburn.Micro中的约定设置为解析
项源(使用
)和SelectedItem(使用
选定的

视图模型:

internal class FuelViewModel : Screen
{
    public FuelViewModel()
    {
        FuelType = Enum.GetValues(typeof(Fueltype)).Cast<Fueltype>().ToList();
    }

    private Fueltype selectedFuelType;

    public Fueltype SelectedFuelType
    {
        get => selectedFuelType;
        set => Set(ref selectedFuelType, value);
    }

    public IReadOnlyList<Fueltype> FuelType { get; }
}
    <ComboBox x:Name="FuelType"/>
内部类FuelViewModel:屏幕
{
公共FuelViewModel()
{
FuelType=Enum.GetValues(typeof(FuelType)).Cast().ToList();
}
选择私有燃料类型Fueltype;
公共燃油类型已选择燃油类型
{
get=>selectedFuelType;
set=>set(参考selectedFuelType,值);
}
公共IReadOnlyList FuelType{get;}
}
查看:

internal class FuelViewModel : Screen
{
    public FuelViewModel()
    {
        FuelType = Enum.GetValues(typeof(Fueltype)).Cast<Fueltype>().ToList();
    }

    private Fueltype selectedFuelType;

    public Fueltype SelectedFuelType
    {
        get => selectedFuelType;
        set => Set(ref selectedFuelType, value);
    }

    public IReadOnlyList<Fueltype> FuelType { get; }
}
    <ComboBox x:Name="FuelType"/>

编辑:

不按照link的建议做的原因是,它通过生成视图控件数据打破了MVVM原则。如果要从简单的枚举备份更改为数据库备份,则视图会中断。使用正确的方法,您可以在不接触视图的情况下更改ViewModel中的类型,也可以在不破坏ViewModel的情况下交换视图。

正确的方法是在ViewModel中创建项目列表和选定项目。Caliburn.Micro中的约定设置为解析
项源(使用
)和SelectedItem(使用
选定的

视图模型:

internal class FuelViewModel : Screen
{
    public FuelViewModel()
    {
        FuelType = Enum.GetValues(typeof(Fueltype)).Cast<Fueltype>().ToList();
    }

    private Fueltype selectedFuelType;

    public Fueltype SelectedFuelType
    {
        get => selectedFuelType;
        set => Set(ref selectedFuelType, value);
    }

    public IReadOnlyList<Fueltype> FuelType { get; }
}
    <ComboBox x:Name="FuelType"/>
内部类FuelViewModel:屏幕
{
公共FuelViewModel()
{
FuelType=Enum.GetValues(typeof(FuelType)).Cast().ToList();
}
选择私有燃料类型Fueltype;
公共燃油类型已选择燃油类型
{
get=>selectedFuelType;
set=>set(参考selectedFuelType,值);
}
公共IReadOnlyList FuelType{get;}
}
查看:

internal class FuelViewModel : Screen
{
    public FuelViewModel()
    {
        FuelType = Enum.GetValues(typeof(Fueltype)).Cast<Fueltype>().ToList();
    }

    private Fueltype selectedFuelType;

    public Fueltype SelectedFuelType
    {
        get => selectedFuelType;
        set => Set(ref selectedFuelType, value);
    }

    public IReadOnlyList<Fueltype> FuelType { get; }
}
    <ComboBox x:Name="FuelType"/>

编辑:
不按照link的建议做的原因是,它通过生成视图控件数据打破了MVVM原则。如果要从简单的枚举备份更改为数据库备份,则视图会中断。使用正确的方法,您可以在不接触视图的情况下更改ViewModel中的类型,也可以在不破坏ViewModel的情况下交换视图。

示例:示例: