我如何打印列表框项目计数以及扩展器 header?

How I can print listbox item count along with Expander header?

我想用扩展器 header 打印列表框项目计数 header1(计数)。我怎样才能在 WPF

中实现这个

您还没有为您所做的事情提供任何代码,例如您是否正在使用 ViewModel 或正在做 code-behind。有多种方法可以解决。

我的 ViewModel 方式

在这个例子中,我制作了一个包含 stringObservableCollection 的 ViewModel 来填充 ListBoxExpander header 绑定到使用 HeaderText 和 ItemCount 的组合填充的 属性。

   public class ViewModel : INotifyPropertyChanged
   {

      public event PropertyChangedEventHandler PropertyChanged;

      private string _headerText = string.Empty;
      private string _headerTextFull = string.Empty;
      private ObservableCollection<string> _listItems = new ObservableCollection<string>();
      private int _itemCount = 0;

      public ViewModel() { }

      public string HeaderText
      {
         get { return _headerText; }
         set
         {
            _headerText = value;
            NotifyPropertyChanged("HeaderText");
            UpdateHeader();
         }
      }

      public string HeaderTextFull
      {
         get { return _headerTextFull; }
         set
         {
            _headerTextFull = value;
            NotifyPropertyChanged("HeaderTextFull");
         }
      }

      public ObservableCollection<string> ListItems
      {
         get { return _listItems; }
         set
         {
            _listItems = value;
            NotifyPropertyChanged("ListItems");
            ItemCount = (_listItems != null ? _listItems.Count : 0);
         }
      }

      public int ItemCount
      {
         get { return _itemCount; }
         set
         {
            _itemCount = value;
            NotifyPropertyChanged("ItemCount");
            UpdateHeader();
         }
      }

      private void UpdateHeader()
      {
         HeaderTextFull = String.Format("{0} ({1})", _headerText, _itemCount);
      }

      public void NotifyPropertyChanged(string propertyName)
      {
         if (PropertyChanged != null)
         {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
         }
      }
   }

XAML为Window:

<Window x:Class="SO37192142.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Expander Grid.Row="0" Name="expander1" Header="{Binding Path=HeaderTextFull, FallbackValue='Items'}" IsExpanded="True">
         <Grid>
            <ListBox Name="listBox1" Width="Auto" Height="Auto" ItemsSource="{Binding Path=ListItems}" />
         </Grid>
      </Expander>
      <Button Grid.Row="1" Content="Add an Item" Click="Button_Click" />
   </Grid>
</Window>

code-behind:

   public partial class Window1 : Window
   {
      ViewModel myModel = new ViewModel();
      public Window1()
      {
         InitializeComponent();

         myModel.ListItems.CollectionChanged += new NotifyCollectionChangedEventHandler(ListItems_CollectionChanged);

         myModel.HeaderText = "Items";
         myModel.ListItems.Add("Item 1");
         myModel.ListItems.Add("Item 2");
         this.DataContext = myModel;
      }

      void ListItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         myModel.ItemCount = myModel.ListItems.Count;
      }

      void Button_Click(object sender, RoutedEventArgs e)
      {
         myModel.ListItems.Add("Another item");
      }

   }

启动时,Expander header 会显示 "Items (2)"。每次单击按钮时,header 将更新以显示新计数。

以上略有不同

这是一个提供上述示例的示例,但还添加了第二个列表以演示不同的方式。注意 Expander.Header 部分。

<Window x:Class="SO37192142.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="*" />
         <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="*" />
         <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>

         <Expander Grid.Row="0" Grid.Column="0" Name="expander1" Header="{Binding Path=HeaderTextFull, FallbackValue='Items'}" IsExpanded="True">
         <Grid>
            <ListBox Name="listBox1" Width="Auto" Height="Auto" ItemsSource="{Binding Path=ListItems}" />
         </Grid>
      </Expander>

       <!-- SECOND EXPANDER THAT DOESN'T RELY ON A VIEWMODEL -->
      <Expander Grid.Row="0" Grid.Column="1" Name="expander2" IsExpanded="True">
         <Expander.Header>
            <StackPanel Orientation="Horizontal">
               <TextBlock Text="{Binding ElementName=listBox2, Path=Items.Count, UpdateSourceTrigger=PropertyChanged, StringFormat={}Items ({0})}" />
            </StackPanel>
         </Expander.Header>
         <Grid>
            <ListBox Name="listBox2" Width="Auto" Height="Auto" ItemsSource="{Binding Path=ListItems}" />
         </Grid>
      </Expander>
      <Button Grid.Row="1" Grid.ColumnSpan="2" Content="Add an Item" Click="Button_Click" />
   </Grid>
</Window>

只有代码隐藏

如果出于某种原因,您只是使用 code-behind,您可以这样做:

   public partial class Window1 : Window
   {
      public Window1()
      {
         InitializeComponent();

         ((INotifyCollectionChanged)listBox1.Items).CollectionChanged += new NotifyCollectionChangedEventHandler(ListBox_CollectionChanged);

      }

      void ListBox_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         expander1.Header = "Items (" + listBox1.Items.Count + ")";
      }

   }