C
C#2y ago
ivi

❔ WPF - Binding to last child of collection

I have the following setup:
public partial class StatusMessageService : ObservableObject
{
[ObservableProperty] private ObservableCollection<string> _messageStack = new()
{
"Welcome to Rainbow Stone"
};

public string? LastMessage => MessageStack.LastOrDefault();

public void AddMessage(string message)
{
MessageStack.Add(message);
}
}
public partial class StatusMessageService : ObservableObject
{
[ObservableProperty] private ObservableCollection<string> _messageStack = new()
{
"Welcome to Rainbow Stone"
};

public string? LastMessage => MessageStack.LastOrDefault();

public void AddMessage(string message)
{
MessageStack.Add(message);
}
}
I'm trying to output LastMessage in a StatusBar control. I am following the solution 2 in: https://stackoverflow.com/a/38076864/2275217 However, the StatusBar does not update with new messages. A comment on SO says: "In case of the property one would want to fire change notifications for said property in case of CollectionChanged." However, this is not quite clear to me. What is meant by that? I am using CommunityToolkit.Mvvm for the Observable Object structure, but it wouldn't let me set [ObservableProperty] on LastMessage because it's a field. How to update LastMessage along with the collection?
Stack Overflow
Binding to last array element
So far, i have an ObservableCollection<T> for objects. I always want to display the last inserted element into a TextBlock. I implemented two solutions in XAML, but both not working: <
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server