Я только начал изучать Avalonia, и я работаю над проектом, где пользователь может создавать диаграммы с разъемами и формами (прямоугольники, эллипсы ....). < /p>
Я решил создать базовый класс формы и, на данный момент, 2 полученных Rectanglemodel и Ellipsemodel. Эти модели в настоящее время пусты.
Важными здесь являются соответствующие виды, которые обладают всеми свойствами, необходимыми для отображения форм в XAML: < /p>
public partial class ShapeViewModel : ObservableObject {
readonly ShapeModel shapeModel;
[ObservableProperty]
private int x;
[ObservableProperty]
private int y;
[ObservableProperty]
private int width;
[ObservableProperty]
private int height;
[ObservableProperty]
private Brush fill;
public Type ShapeType { get => shapeModel.GetType(); }
public ShapeViewModel(ShapeModel shapeModel, int x, int y, int width, int height, Brush fill) {
this.shapeModel = shapeModel;
X = x;
Y = y;
Width = width;
Height = height;
Fill = fill;
}
}
< /code>
Shapemodel, опять же, бесполезен на данный момент как свойство Shapetype.public partial class RectangleViewModel : ShapeViewModel {
public RectangleViewModel(ShapeModel shapeModel, int x, int y, int width, int height, Brush fill) : base(shapeModel, x, y, width, height, fill) { }
}
< /code>
Я создаю «DrawingsView» и «DrawingsViewModel»: < /p>
public partial class DrawingsViewModel : ObservableObject {
[ObservableProperty]
private ObservableCollection shapes;
public DrawingsViewModel() {
Shapes = new ObservableCollection(); //just for testing
Shapes.Add(new RectangleViewModel(new RectangleModel(), 100, 100, 200, 200, new SolidColorBrush(Colors.Beige)));
Shapes.Add(new EllipseViewModel(new EllipseModel(), 200, 200, 200, 200, new SolidColorBrush(Colors.Blue)));
}
[RelayCommand]
public void AddShape(ShapeViewModel shape) {
Shapes.Add(shape);
}
}
< /code>
< /code>
In order to differentiate the objects in the Shapes collection of the vm I created the following templates in the App.axaml:
< /code>
The shapes are indeed correctly differentiated and colored, and the Visual tree for the Shapes is correctly "DrawingViews -> ... -> ItemPresenter -> Canvas -> ContentPresenter -> Rectangle (or Ellipse).
The problem is that I'm not able to set the bounds of the Shapes. They are always going to appear at (x,y) = (0,0).
I have tried using a Selector like:
< /code>
but it doesn't work, since the properties to which I bind to are not in the view DataContext.
Подробнее здесь: https://stackoverflow.com/questions/796 ... n-avalonia
Использование DataTemplate с различными формами в авалонии ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение