How to bind SelectedItems.Count and Items.Count of ListBox in ViewModel's property?

Emon Haque 3,176 Reputation points
2020-10-03T20:14:48.167+00:00

In xaml this works:

<TextBlock HorizontalAlignment="Right">
    <Run Text="{Binding ElementName=lb, Path=SelectedItems.Count, Mode=OneWay}"/>
    <Run Text="/"/>
    <Run Text="{Binding ElementName=lb, Path=Items.Count, Mode=OneWay}"/>
    <Run Text=" excluded"/>
</TextBlock>

Can I bind these to properties in ViewModel?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,701 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emon Haque 3,176 Reputation points
    2020-10-04T14:17:44.5+00:00

    Not that hard but it took a while to figure out! I've to extend a ListBox with two DependencyProperty, Total and Selected. In OnSelectionChanged I've to set the Selected = SelectedItems.Count and in OnItemsChanged I've to set Total = Items.Count. Those Total and Selected can be bound to ViewModel's property.

    0 comments No comments

0 additional answers

Sort by: Most helpful