2016-07-02 8 views
0

私はWPFにはかなり新しく、ラベルを追加する方法を理解しようとしましたが、現在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

'ListView'内部' Label'を入れてのポイントは何ですか?再利用可能なソリューションが必要な場合は、 'Label'と' ListView'を1つのコンポーネントで組み合わせた '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);   
    } 
関連する問題