如何打印列表框项目计数以及Expander标题?

时间:2016-05-12 16:04:43

标签: wpf expander

我想用扩展器的标题打印列表框项目计数alog header1(计数)。我怎样才能在WPF中实现这一目标

1 个答案:

答案 0 :(得分:1)

您尚未提供任何代码,例如您使用ViewModel或执行代码隐藏。有多种方法可以解决这个问题。

我的ViewModel Way

在此示例中,我创建了一个包含ObservableCollection string的{​​{1}}的ViewModel来填充ListBoxExpander标头绑定到使用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 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>

代码隐藏:

   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标题会显示“Items(2)”。每次单击该按钮时,标题都将更新以显示新计数。

上面的轻微变化

这是一个提供上述示例的示例,但还添加了第二个列表以演示不同的方式。请注意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>

仅代码

如果由于某种原因,你只是使用代码隐藏,你可以这样做:

   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 + ")";
      }

   }