I have a WPF Data Grid bound to an observable collection, which is working as intended.
What I am trying to do now is add text below it to say: “Number of selected rows: {count goes here}“
What’s the proper way to do this? I could add a new property in the View Model called SelectedCount or something similar and bind to that, but it doesn’t feel right. It seems redundant. Also, I could set the label text dynamically in the code behind, but I’m not sure if that’s the “right” place to do this either.
Here’s an example below.
EDIT:
Please pretend there’s a checkbox column header whose intention is to provide check/uncheck all functionality. The state of this header checkbox should not count towards the final count.

You could use element binding to declaratively bind to the
SelectedItems.Countproperty in XAML:Update
Presumably you’re using MVVM, so adding a
SelectedXCountproperty to your view model is a perfectly reasonable application of the view model. The advantage of having it in the view model is that you could unit test based on the number of selected items. E.g. if you want to check that the user can only progress (aCanNextproperty returns true) if the user has selected some items.The
SelectedItemsproperty is not aDependencyPropertyso can’t be bound to, but there are many articles online that get around the issue when using the DataGrid in MVVM. Most of the solutions involve using a mechanism for calling a view model command on the invocation of the DataGrid’sSelectionChangedevent.