我有一个枚举
public enum FuelType
{
Diesel,
Petrol,
E10
}
如何将其与Caliburn.Micro
的Combobox绑定 xaml:<ComboBox x:Name="Fuel" Grid.Row="5" Grid.Column="2" Margin="3"/>
和ModelView中的属性:
public FuelType Fuel
{
get { return _fuel; }
set
{
_fuel = value;
NotifyOfPropertyChange(nameof(Fuel));
}
}
答案 0 :(得分:1)
执行此操作的正确方法是在ViewModel中包含项目列表和所选项目。 Caliburn.Micro中的约定设置为解析ItemsSource
(使用<x:Name>
)和SelectedItem(使用Selected<x:Name>
)。
<强>视图模型:强>
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"/>
修改强>
不做Sybren链接建议的原因是它通过制作View控件数据来打破MVVM原则。如果您要从简单的枚举备份更改为支持View中断的数据库。使用正确的方法,您可以在不触及视图的情况下更改ViewModel中的类型,也可以在不破坏ViewModel的情况下交换视图。