© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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
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
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]
[ObservableProperty]
on
LastMessage
LastMessage
because it's a field.

How to update
LastMessage
LastMessage
along with the collection?
image.png
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:

<
Binding to last array element
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

WPF binding
C#CC# / help
13mo ago
❔ WPF Binding to collection in static instance not updating list
C#CC# / help
3y ago
Wpf binding failures
C#CC# / help
15mo ago
❔ WPF data binding
C#CC# / help
3y ago