✅ 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 it
55 Replies
leowest
leowest3w ago
You would have that List fill a ObservableCollection and u would bind it to the ItemSource of your ItemsControl
Somgör from Human Resources
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
leowest
leowest3w ago
<ItemsControl ItemsSource="{Binding Chats}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="0,10,0,0"
CornerRadius="5"
BorderBrush="Gray" BorderThickness="1"
Padding="5">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}"/>
<TextBlock Margin="5 0" FontWeight="Bold"
Text="{Binding Username}"/>
<TextBlock Margin="5 0"
Text="{Binding Message}"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Chats}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Margin="0,10,0,0"
CornerRadius="5"
BorderBrush="Gray" BorderThickness="1"
Padding="5">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}"/>
<TextBlock Margin="5 0" FontWeight="Bold"
Text="{Binding Username}"/>
<TextBlock Margin="5 0"
Text="{Binding Message}"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Somgör from Human Resources
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
leowest
leowest3w ago
<ItemsControl ItemsSource="{Binding Chats}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<view:ChatComponent/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding Chats}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<view:ChatComponent/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Somgör from Human Resources
Yes like that My laptop is back on I'll share my code in a moment
leowest
leowest3w ago
it should inherit the context from the itemssource
Somgör from Human Resources
this is the part that fetches my data (in this case its Communities but it serves the example just as well)
No description
Somgör from Human Resources
this is the view that should display the data
No description
leowest
leowest3w ago
u should do none of that in your constructor
Somgör from Human Resources
hm where would you recomment i do that
leowest
leowest3w ago
a load event or onopen or something
Somgör from Human Resources
this is my commponent view along with its viewmodel
No description
No description
Somgör from Human Resources
and inside of that components ViewModel i need to somehow fetch the current chat / community
leowest
leowest3w ago
u shouldn't have a vm for that if its inheriting
leowest
leowest3w ago
if u do it will overwrite the inherited context
Somgör from Human Resources
ah so like
leowest
leowest3w ago
this is my commponent view along with its viewmodel
Somgör from Human Resources
i can bind the object and if i give the member name of the object it will assign it
leowest
leowest3w ago
No description
leowest
leowest3w ago
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
Somgör from Human Resources
what should i be doing
leowest
leowest3w ago
not giving it a vm
Somgör from Human Resources
so i can safely delete the VM class for it?
leowest
leowest3w ago
if its a dumb usercontrol then it doesnt need a vm not sure what safely means
Somgör from Human Resources
delete it and it still works properly
leowest
leowest3w ago
it should, I wouldn't name it a view thou its not
Somgör from Human Resources
but where could i handle the component? in its codebehind?
leowest
leowest3w ago
handle what?
Somgör from Human Resources
cause i need to download / fetch the image from an URL first before setting it
leowest
leowest3w ago
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
Somgör from Human Resources
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:
leowest
leowest3w ago
wpf is like 20 years old and can why wouldn't avalonia
Somgör from Human Resources
: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
leowest
leowest3w ago
yes u should be able to set the datatype in the datatemplate and in the axaml
Somgör from Human Resources
i mean like my Community object has a member called CommunityName which is a string
Somgör from Human Resources
does this mean this is valid in my component because the data template is my community?
No description
leowest
leowest3w ago
x:DataType="model:Community" yes it inherits it but u can further define it as well
Somgör from Human Resources
i cant access the namespace the object is in
leowest
leowest3w ago
why not
Somgör from Human Resources
it says it cant resolve my namespace from https://github.com/avaloniaui
leowest
leowest3w ago
well I would need more info that just that
Somgör from Human Resources
me too :SCgetoutofmyhead:
Somgör from Human Resources
trying to set the DataType in my component view
No description
leowest
leowest3w ago
post your community post your xmlns
Somgör from Human Resources
uh ah :SCgetoutofmyhead: i got it working i didnt set an xmlns that references the Objects namespace
leowest
leowest3w ago
well there u go
Somgör from Human Resources
:soPortuguese: btw i get what Binding does
Somgör from Human Resources
but what does it mean when i dont pass anything behind Binding?
No description
leowest
leowest3w ago
tbh u dont need that that is just like explicitly saying it will inherit the context of the itemssource
Somgör from Human Resources
aaah i see
leowest
leowest3w ago
which it does implicitly
Somgör from Human Resources
and i dont need it cause it does it automatically? ah yeees thank youuu