2016-07-02 2 views
0

저는 WPF에 새로 익숙해졌으며 레이블을 추가하는 방법을 알아 내려고했습니다. 현재 ListView에있는 항목의 수를 보여주는 다음 ListView 안에 나타납니다. 상단에 ListView 패딩을 주어 Label을위한 공간을 마련했습니다.XAML의 ListView 내부에 레이블 중첩시키기

<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'를 결합한'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);   
    } 
관련 문제