2016-07-02 38 views
0

我是相當新的WPF,我已經嘗試找出如何添加一個Label出現下面的ListView這表明目前在ListView的項目數內。我在頂部給ListView填充以爲標籤騰出空間。嵌套一個ListView內的標籤在XAML

<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> 

如果有人可以幫助我,它將不勝感激。

+0

什麼是把'Label'的'ListView'內的點?如果你想要一個可重用的解決方案,爲什麼不編寫一個在單個組件中結合了一個'Label'和'ListView'的UserControl?你可能可以一起破解默認的'ListView.Template',但是IME通常比這樣的場景更值得痛苦。請更準確地解釋爲什麼'Label' _ has_成爲ListView的一部分。還請提供一個很好的[mcve],以顯示您已經嘗試過的內容,並清楚說明您獲得該功能的具體問題。 –

+0

我想這是可行的,請張貼預期結果的一些屏幕截圖。 – pushpraj

回答

1

ListBoxCount output

  1. 編輯的ListBoxTemplate。您可以通過右鍵單擊Document outline部分中的ListBox來執行此操作。並添加您的Label如下。

    ... 
    <ScrollViewer Focusable="false" Padding="{TemplateBinding Padding}"> 
        <StackPanel> 
         <Label uc:Window2.CountFor="False" /> 
         <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </StackPanel> 
    </ScrollViewer> 
    ... 
    
  2. 我寫的附加屬性CountFor。代碼如下給予:

    #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 
    
0

您的解決方案很簡單,您可以創建一個int來計算標籤中的項目數量,然後分配一個新的文本塊,也可以完全跳過文本塊並簡單地添加int,檢查以下代碼:

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