✅ Help with Avalonia databinding and library objects on ViewModels
Lets say I have an API Client that allows me to fetch data like chats, messages etc
That API Client responds with a List of the object which i use to construct my component (in this case for example a chat with a simple name, user icon and last message sent in it)
Now i bind the
<ItemsControl>
to my List of fetched chats and set the datatemplate to my Chat component
But now in my ViewModel class for said Chat i need to access the current chat that is being used to construct my View (the chat component) so i can further manipulate and process it55 Replies
You would have that List fill a ObservableCollection
and u would bind it to the ItemSource of your ItemsControl
Yes I have an ObservableCollection of the type of my Chats that my view binds the Items Control to
I'll show you the code in a moment I'm running a system update rn lmao
The thing is
That List / the data template refers to a View that I have seperated to keep that chat component Re usable cause it's very likely I'll use that again in the project
Yes like that
My laptop is back on I'll share my code in a moment
it should inherit the context from the itemssource
this is the part that fetches my data (in this case its Communities but it serves the example just as well)
this is the view that should display the data
u should do none of that in your constructor
hm
where would you recomment i do that
a load event or onopen or something
this is my commponent view along with its viewmodel
and inside of that components ViewModel i need to somehow fetch the current chat / community
u shouldn't have a vm for that if its inheriting
uh
wym
if u do it will overwrite the inherited context
ah
so like
this is my commponent view along with its viewmodel
i can bind the object and if i give the member name of the object it will assign it
?
you're already giving it a context
if u add a vm to it with a viewlocator it will overwrite that
and u lose the former
what should i be doing
not giving it a vm
so i can safely delete the VM class for it?
if its a dumb usercontrol then it doesnt need a vm
not sure what safely means
delete it and it still works properly
it should, I wouldn't name it a view thou its not
but where could i handle the component? in its codebehind?
handle what?
cause i need to download / fetch the image from an URL first before setting it
in your service
either give the url to Source and let avalonia handle it
or make it a stream or something usable and feed it that
this is my plan:
-> fetch communities -> each community fetches its image and stores it in a cache folder -> set image
can avalonia handle URLs?
:plink:
wpf is like 20 years old and can why wouldn't avalonia
:soPortuguese:
and in my Component View axaml i can just access the members of the object it has the datatype of?
cause it wont show up with any auto completion or anything
yes u should be able to set the datatype in the datatemplate and in the axaml
i mean like
my Community object has a member called
CommunityName
which is a stringdoes this mean this is valid in my component because the data template is my community?
x:DataType="model:Community"
yes
it inherits it but u can further define it as well
i cant access the namespace the object is in
why not
it says it cant resolve my namespace from https://github.com/avaloniaui
well I would need more info that just that
me too :SCgetoutofmyhead:
trying to set the DataType in my component view
post your community
post your xmlns
uh
ah
:SCgetoutofmyhead:
i got it working
i didnt set an xmlns that references the Objects namespace
well there u go
:soPortuguese:
btw
i get what Binding does
but what does it mean when i dont pass anything behind Binding?
tbh u dont need that
that is just like explicitly saying it will inherit the context of the itemssource
aaah
i see
which it does implicitly
and i dont need it cause it does it automatically?
ah
yeees
thank youuu