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