[AVALONIA] Polylines not displaying on Canvas
I have been having this issue for a while. My Polylines which I have generated are not being displayed in the Canvas.
I am using MVVM so I have created all the Models, ensured they are correctly populating, checked that they are inside the canvas using the F12 debugging tool but they just are not visible. Help?
Canvas Model
Panel View Model
45 Replies
XAML View
Main Window View
Results
Wrong XAML layout. Your
ItemsControl
should be top and use an ItemsPanel
for the Canvas
. Check https://docs.avaloniaui.net/docs/concepts/custom-itemspanel for specificsAttached properties like
Canvas.Left
only get inspected when they're a direct child of the Canvas
control.Appreciate that. I’ll give it a try
No change 😦
XAML Code
Are you using compiled bindings?
can you elaborate a bit?
im just grabbing the value of a list variable of polylines in c#
looking at the docs i dont believe i am, wouldnt have a clue how to use them
like this?
That's one way. You should typically enable them project wide in csproj and use
{ReflectionBinding}
if you're limited in some scenario.
I need to leave for awhile though.your allgood. still didnt work but thanks for your help this far 🙂
This is weird because you're storing
Polyline
controls. You're also storing them in properties that don't raise INPC and in a collection that doesn't raise INotifyCollectionChanged
. So you need to be very careful about initialization order and when the binding happens.
You can try a ContentControl
instead, but I'm not entirely sure what happens when you try to wrap a control in it.
All I want to do is get a bunch of Polylines and smash them onto a canvas
how would you implement INotifyCollectionChanged to this?
What Mvvm framework are you using?
Avalonia.
Also is this an issue?
Avalonia is a GUI framework, not an MVVM framework.
oh then .NET i think
That's also not an MVVM framework.
How did you create your project? Avalonia templates (the MVVM one at least) lets you choose because ReactiveUI and Mvvm Toolkit.
then Reactive i suppose
sorry its one of the first times ive used model view
You can probably get around it for now by doing:
But I'm guessing
CanvasSegment
is in another collection, so that probably needs changed too.it is when i begin making multiple different "Panels"
but at the moment its only used as a singular instance
This Model is unused at the moment but will be in the future
I really suggest you work on some sample apps instead. Like basic data entry on a form so you can understand what
INotifyPropertyChanged
does, how bindings actually work, etc.
You simply can't work with the UI this way unless you refresh everything on any change. And change tracking without specific logic everywhere is hard. Which is an option (I do this for a vector art app, but still have change notifications), but it's not ideal in all cases.i knew that a refresh everything would be the case but i havent got any other option im afraid
at least not one that i have thought of
It's different in my case because everything is custom drawn via Skia. I'm not using UI controls.
i dont necessarily need to have these controls be interacted with. its purely just a line that needs to be on the ui, everything logic and interaction wise can pretty much be done seperately from the lines that are drawn on
If you're using UI controls (provided by Avalonia), you'll need to recreate the entire hierarchy, write a lot of synchronization code, or explicitly set
DataContext = null; DataContext = vm;
to force all bindings to update...which recreates a lot of controls if you're using collection controls.
^ I mean without using proper MVVM techniques with INotifyPropertyChanged
.Oh i see. you scared me a bit. Yea well im still learning so I will definitely look at the
INotifyPropertyChanged
The basics of what I need from this specific part of the application are just Lines drawn onto the UI that I can change the look of (size, length etc) and also apply a sort of "collision detection" to. Best way to describe the need for the "collision detection" is if you think of a volume slider how it follows a path, now make that path able to change to a diagonal or whichever direction it needs to go to follow the "track"
the lines represent a railway track and basically i need to be able to "slide a train" along the railway track and it follows the path
long story lolIf you're using ReactiveUI, then it provides an implementation via
ReactiveObject
and this.RaiseAndSetIfChanged
or whatever.
I find Mvvm Toolkit to be much more intuitive and easier to use though.reckon for a newbie that its better if i switch to that rather than Rx?
IMO, a beginner isn't going to be learning how to use observables...which is a core part of Rx and really the only reason why you'd use it over (or side-by-side with) Mvvm Toolkit.
So I would say yes.
Okay sweet ill give that a go. Ill do some readup on this
INotify...
and see if i can get somewhere
im guessing that I would have to recreate the project to change toolkits? unless you know a way to do it without that option?It's easier to recreate if you don't have any experience.
fun times 🙂
Cheers for the help. Ill let you know how it goes i suppose
I would still recommend some basic practice regardless.
It's not obvious to beginners to know when INPC matters, but it's basically: anytime a property is being changed from code or is binded to 2 or more controls.
And if that property change intends to affect the UI then it needs INPC?
In your ViewModels, it's more straightforward to make all properties respect INPC or otherwise be get-only.
The case where you don't need it is when the UI updates the property in the VM. Say you bind a
string
to a TextBox.Text
. When the user types, the TextBox
will update the property. Which is fine, no notifications needed because no other controls need to be aware of the change. TextBox
already knows.
However, say you do MyBindedText = "Testing123";
in your VM, then the TextBox
needs a change notification to let it know the property changed.Oh i got you, so if its "UI to Code to UI" then no need for INPC however if its "Code to UI" then yes
"UI to Code" No
"Code to UI" Yes
"UI to Code to UI" Yes
yea i realised i put it in a bad way
i got you tho haha
It's easier to just implement it for all cases when you're starting out.
Because it's easy to find yourself in a 30 minute debugging session over something dumb.
try a week on this shit just to figure out i need INPC and a new MVVM toolkit.....i love my hobby 😆
its algood tho. I knew this wouldnt be easy at all. I just needed a way to keep my software skills fresh because I havent had any use for them since leaving study and covid forcing me to choose a different path of work
Fixed it up 🙂