2014-03-13 58 views
14

有沒有什麼辦法可以獲得當前ItemsControl項目的索引WPF如何獲取當前ItemsControl項目的索引?

例如,我想要做的事,如:

<ItemsControl> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBox Text="{Binding current_index}"> 
      </TextBox> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

所以在此之後,第一TextBox會顯示文字"0",第二"1",第三"2" ...

+0

可能重複的[WPF ItemsControl的當前列表項中的索引的ItemsSource](http://stackoverflow.com/questions/6511180/wpf-itemscontrol-the-current-listitem-index-in-the-itemssource ) – har07

回答

25

我建議看:

WPF ItemsControl the current ListItem Index in the ItemsSource

它說明了如何解決的事實,沒有一個建在ItemsControl的索引屬性。

編輯:

我嘗試下面的代碼:

<Window.Resources> 
    <x:Array Type="{x:Type sys:String}" x:Key="MyArray"> 
     <sys:String>One</sys:String> 
     <sys:String>Two</sys:String> 
     <sys:String>Three</sys:String> 
    </x:Array> 
</Window.Resources> 
<ItemsControl ItemsSource="{StaticResource MyArray}" AlternationCount="100" > 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
       RelativeSource={RelativeSource TemplatedParent}, 
       StringFormat={}Index is {0}}"> 
      </TextBlock> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl > 

,並得到一個窗口有三個的TextBlocks這樣的:

[Index is 0] 
[Index is 1] 
[Index is 2] 
+1

對於其他誰找到這個,我喜歡和使用Saykor的解決方案。我聽說你不能總是從零開始依靠AlternationCount。感覺像一個黑客。而是使用轉換器並將確定索引所需的信息傳遞給它。 – Skychan

+1

@Skychan你能否提供更多的參數,爲什麼用這種方式的AlternationCount應該是不可靠的? – Leonidas

6

檢查了這一點

<ItemsControl ItemsSource="{Binding Items}" Name="lista"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical"> 
        <TextBlock> 
         <TextBlock.Text> 
          <MultiBinding Converter="{StaticResource converter}"> 
           <Binding Path="."/> 
           <Binding ElementName="lista" Path="ItemsSource"/> 
          </MultiBinding> 
         </TextBlock.Text> 
        </TextBlock> 
       </StackPanel> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

轉換器看起來像這樣

public class conv : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     ObservableCollection<string> lista = (ObservableCollection<string>)values[1]; 
     return String.Concat(lista.IndexOf(values[0].ToString()), " ", values[0].ToString()); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

結果 enter image description here

+1

請注意,只有在視圖與原始來源完全匹配時纔有效,即不會對其進行排序或過濾。 –

3

在這裏,我怎麼弄的ItemIndex

<ItemsControl> 
     <ItemsControl.Resources> 
      <CollectionViewSource x:Key="ProductItems" Source="{Binding SelectedScanViewModel.Products}"> 
       <CollectionViewSource.SortDescriptions> 
        <componentModel:SortDescription PropertyName="ProductName" Direction="Ascending"/> 
       </CollectionViewSource.SortDescriptions> 
      </CollectionViewSource> 
     </ItemsControl.Resources> 
     <ItemsControl.ItemsSource> 
      <Binding Source="{StaticResource ProductItems}"/> 
     </ItemsControl.ItemsSource> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <StackPanel HorizontalAlignment="Center"> 
        <TextBlock Text="{Binding ProductName}" HorizontalAlignment="Center" /> 
        <TextBox Name="txtFocus" Text="{Binding Qty}" MinWidth="80" HorizontalAlignment="Center" 
            behaviors:SelectTextOnFocus.Active="True"> 
         <TextBox.TabIndex> 
          <MultiBinding Converter="{StaticResource GetIndexMultiConverter}" ConverterParameter="0"> 
           <Binding Path="."/> 
           <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}" Path="ItemsSource"/> 
          </MultiBinding> 
         </TextBox.TabIndex> 
        </TextBox> 
       </StackPanel> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="{Binding SelectedScanViewModel.Products.Count}"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 

和轉換器:

public class GetIndexMultiConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     var collection = (ListCollectionView)values[1]; 
     var itemIndex = collection.IndexOf(values[0]); 

     return itemIndex; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException("GetIndexMultiConverter_ConvertBack"); 
    } 
} 

通過這種方式,您可以將每種類型的集合綁定到ItemSource,並且他將更改爲ListCollectionView。所以轉換器將適用於不同的收集類型。

xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
+0

這是一個類似的替代解決方案,它使用ItemContainerGenerator來確定索引。 http://stackoverflow.com/questions/7290147/is-possible-to-get-a-index-from-a-item-in-a-list – Skychan

+0

我認爲這種方法將無法正常工作:IndexOf使用Equals如果源集合有重複(例如兩個相同的字符串值),IndexOf將始終返回第一個項目。 – aderesh

+0

我認爲使用ItemContainerGenerator的解決方案更好。謝謝。 – aderesh

相關問題