我有一个枚举
public enum FuelType
{
Diesel,
Petrol,
E10
}
我如何使用 Caliburn.Micro 将它绑定到 Combobox
xml: <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));
}
}
正确的方法是在 ViewModel 中有一个项目列表和所选项目。Caliburn.Micro 中的约定被设置为解决ItemsSource
(using <x:Name>
) 和 SelectedItem (using 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 原则。如果您要从简单的枚举支持更改为支持您的视图的数据库,则会中断。使用正确的方法,您可以在不接触视图的情况下更改 ViewModel 中的类型,还可以在不破坏 ViewModel 的情况下交换视图。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句