2016-09-14 86 views
1

我想使用WPF在TextBlock中綁定ItemControl的ItemsControl的ItemsSource的CountSource。使用WPF在TextBlock中綁定項目ItemsControl的ItemsSource

看看到我試過代碼

<Menu> 
    <MenuItem> 
     <MenuItem.Header> 
      <TextBlock Text="{Binding Path=(ItemsControl.ItemsSource.Item, RelativeSource={RelativeSource TemplatedParent}}" /> 
     </MenuItem.Header> 

     <ItemsControl ItemsSource="{Binding PersonCollection}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate > 
        <StackPanel Orientation="Horizontal" Margin="2" MinWidth="100"> 
         <TextBlock Text="{Binding Value.Text}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </MenuItem> 
</Menu> 

注:我需要得到由Collection.Count物業基於ItemsControl ItemsSource不計。請幫助我。

+0

什麼阻止你從'ItemsCource'而不是'ItemsSource'獲取'PersonCollection'的Count? – Gopichandar

+0

@Gopichandar我在自定義控件中使用這個。所以,我需要根據ItemsSource Count來做出決定。請按照我的要求幫助我。 –

+0

試一試'Path = Items.Count' – Gopichandar

回答

2

這是解決方案:

<Menu> 
     <MenuItem> 
      <MenuItem.Header> 
       <TextBox Text="{Binding ElementName=ItemsControl, Path=Items.Count, Mode=OneWay}" /> 
      </MenuItem.Header> 

      <ItemsControl x:Name="ItemsControl" 
          ItemsSource="{Binding Items}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal" 
            Margin="2" 
            MinWidth="100"> 
          <TextBlock Text="{Binding Value.Text}" /> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </MenuItem> 
    </Menu> 

它是否適合你?

+0

我需要基於'ItemsControl ItemsSource'而不是'Collection.Count'屬性來計數。請幫助我。 –

+0

這是相同的價值。相同的號碼 –

+0

是的都是一樣的。但我需要這樣, –

相關問題