2013-10-21 21 views
5

これは他の類似の質問がいくつかあることを知っていますが、ListBoxまたはListViewで動作するようにAlternationIndexを取得することには本当に問題があります。なぜListBox AlternationIndexは常に0を返すのですか

私のXAMLは、次のとおりです。

  <ListBox BorderThickness="0" Name="RecentItemsListBox" HorizontalAlignment="Stretch" 
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
        ItemsSource="{Binding Path=RecentFilesList}" AlternationCount="100"> 
       <ListBox.ItemsPanel> 

        <ItemsPanelTemplate> 
         <WrapPanel /> 
        </ItemsPanelTemplate> 
       </ListBox.ItemsPanel> 

       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
            RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource IncCnvrtr}}" 
             Foreground="DimGray" FontSize="20" FontWeight="Bold" 
             HorizontalAlignment="Left" Margin="5,5,15,5" /> 
          <StackPanel VerticalAlignment="Center"> 
           <TextBlock Text="{Binding ClassName}" Foreground="Black" /> 
           <TextBlock Text="{Binding DisplayName}" Foreground="Black" /> 
          </StackPanel> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

コンバータが1で値をインクリメントこれは正常に動作し、私は、コンバータに送られる値を確認するために、それをデバッグしているが常に0

ですクレイジーなのはこれはListBoxまたはListViewのためだけです

すぐに私がItemsControlに変更すると、インデックスは正しくなりますが、アイテムコントロールは必要ありません。それに付属のすべての機能を持つリストボックスが必要です。

なぜこのようなことが起こっているのかご存じの方は、お手数をおかけします。 ListBoxまたはListViewについては

おかげ

キーラン

答えて

13

あなたは以下のようにListBoxItem/ListViewItemのプロパティを見つける必要があります。

 <TextBlock Text="{Binding Path=(ItemsControl.AlternationIndex), 
         RelativeSource={RelativeSource AncestorType=ListBoxItem}, Converter={StaticResource IncCnvrtr}}" 
         Foreground="DimGray" FontSize="20" FontWeight="Bold" 
         HorizontalAlignment="Left" Margin="5,5,15,5" /> 

差がItemsControlのみを生成していることによるものです項目のコンテナになる単一のContentPresenter、および同じContentPresenterもDataTemplateをロードしています。

しかしListBoxため、ListBoxItemは、アイテムのコンテナであるとDataTemplateListBoxItemTemplateContentPresenterによってロードされます。したがってListBoxItemItemsControl.AlternationIndexプロパティの値はインデックスに応じて変更されますが、DataTemplateをロードするContentPresenterItemsControl.AlternationIndexプロパティの値は常に0になります。これはデフォルト値です。

+0

これは意味があり、ListBoxItemを参照すると完璧に動作します。ありがとうございます。 – Kezza