سؤال

I have spent wayyyy too much time on this, so if someone can help point me in the right direction, I would be very appreciative.

I have an Expander with a ListBox in it. I have bound the ListBox to a List ie FilteredProjects. I would like the Header of the expander to reflect the number of items in the displayed list. I did implement INotifyPropertyChanged for ProjectsCount, and the ItemsSource for the ListBox updates just fine. Thanks in advance for your time.

        <Expander>
            <Expander.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Projects" />
                        <Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="#FF545F42" Margin="1,0,0,0" >
                            <Label Content="{Binding Path=ProjectsCount, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource filter}}" Padding="0" FontSize="8" FontStyle="Italic"></Label>
                        </Border>
                    </StackPanel>
                </DataTemplate>
            </Expander.HeaderTemplate>
            <ListBox x:Name="ProjectsFilterListView" Opacity="1" ItemsSource="{Binding FilteredProjects}"  >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding IsSelected}"  Content="{Binding ItemName}" Margin="10,10,0,10"></CheckBox>
                    </DataTemplate>
                    </ListBox.ItemTemplate>
            </ListBox>
        </Expander>
هل كانت مفيدة؟

المحلول

You could just get the Item Count directly from the ListBox

<Label Content="{Binding Path=Items.Count, ElementName=ProjectsFilterListView}" />

Here is an example of it working, I replaced Label for TextBlock so I could use StringFormat to add "Items:" text before the number as I am not sure what your Converter is doing

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Projects" />
    <Border CornerRadius="4" Padding="0" BorderThickness="1" BorderBrush="Black" Background="Red" Margin="1,0,0,0" >
        <TextBlock Text="{Binding Path=Items.Count, ElementName=ProjectsFilterListView, StringFormat={} Items: {0}}" Padding="0" FontSize="10" FontStyle="Italic" Foreground="Black"/>
    </Border>
</StackPanel>

Result:

enter image description here

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top