协方差和MVVM

本文关键字:MVVM 方差 | 更新日期: 2023-09-27 18:20:55

有一个类Shape从他那里继承了三角形、椭圆等。问题如何更改视图中的图形。当我在ViewModel 中更改数字时

public class MainViewModel : NotificationObject
{
    private Shape shape;
    public MainViewModel()
    {
        Shape = new Ellipse;
        ChangeCurrentShapeCommand = new DelegateCommand(ChangeCurrentShape);
    }
    public Shape Shape
    {
        get
        {
            return this.shape;
        }
        set
        {
            this.shape = value;
            this.RaisePropertyChanged(() => this.Shape);
        }
    }
    public DelegateCommand ChangeCurrentShapeCommand { get; set; }
    private void ChangeCurrentShape()
    {
        Shape = new Triangle;
    }
}

当我在屏幕上调用ChangeCurrentShapeCommand时,椭圆变为三角形。

协方差和MVVM

1)从视图绑定到Shape

2) 为每种形状类型(三角形、椭圆等)定义数据模板