Michael Fich
Michael Fich

Reputation: 506

Nesting a Label inside a ListView in XAML

I am pretty new to WPF and I have tried figuring out how to add a Label appear inside a the following ListView which shows the number of Items currently in the ListView. I've given the ListView padding on the top to make room for the Label.

<ListView x:Name="MyListView" Grid.Row="0" Grid.Column="0" Margin="0,40,0,0" Padding="0" HorizontalAlignment="Stretch"
          VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="{Binding DatasetCode}" FontWeight="Bold"/>
            </WrapPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

If anyone can help me out, it would be greatly appreciated.

Upvotes: 0

Views: 782

Answers (2)

AnjumSKhan
AnjumSKhan

Reputation: 9827

ListBoxCount output

  1. Edit the Template of ListBox. You can do this by Right-Clicking the ListBox in the Document outline section. And add your Label as below.

    ...
    <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}">
        <StackPanel>
            <Label uc:Window2.CountFor="False" />
            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
        </StackPanel>
    </ScrollViewer>
    ...
    
  2. I have written an attached property CountFor . Code is give below :

        #region CountFor attached property
    
    
        public static bool GetCountFor(DependencyObject obj)
        {
            return (bool)obj.GetValue(CountForProperty);
        }
    
        public static void SetCountFor(DependencyObject obj, bool value)
        {
            obj.SetValue(CountForProperty, value);
        }
    
        // Using a DependencyProperty as the backing store for CountFor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CountForProperty =
            DependencyProperty.RegisterAttached("CountFor", typeof(bool), typeof(Window2), new PropertyMetadata(false, new PropertyChangedCallback(GetCountForChanged)));
    
        private static void GetCountForChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue == false) return;
    
            Label lbl = (Label)d;
            lbl.Loaded += (o, args) =>
            {
                DependencyObject parent = VisualTreeHelper.GetParent(lbl);
                while (parent.GetType() != typeof(ListBox))
                    parent = VisualTreeHelper.GetParent(parent);
    
                ListBox lb = (ListBox)parent;
    
                ICollectionView view = CollectionViewSource.GetDefaultView(lb.ItemsSource);
                lbl.Content = "Number of items = " + ((ListCollectionView)view).Count;
    
                view.CollectionChanged += (col, colargs) =>
                {
    
                    lbl.Content = "Number of items = " + ((ListCollectionView)col).Count;
    
                    System.Diagnostics.Debug.WriteLine(((ListCollectionView)col).Count.ToString());
    
                };
            };
        }
    
        #endregion  
    

Upvotes: 1

Dark Templar
Dark Templar

Reputation: 1155

Your solution is simple, you could just create an int to count the number of items in your label and then assign a new textblock, you could also completely skip the textblock and simply add the int, check this code:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int testcounter;
        testcounter = listBox.Items.Count;
        TextBlock BlockCounter = new TextBlock();
        BlockCounter.Text = testcounter.ToString();
        listBox.Items.Add(BlockCounter);           
    }

Upvotes: 0

Related Questions