回答
有幾種方法可以做到這一點,包括some workarounds using the AlternationIndex。
但是,因爲我已經用於其他目的的AlternationIndex我希望得到的元素索引的綁定有以下:
<MultiBinding Converter="{StaticResource indexOfConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}" />
<Binding Path="."/>
</MultiBinding>
當轉換器被定義爲:
public class IndexOfConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (Designer.IsInDesignMode) return false;
var itemsControl = values[0] as ItemsControl;
var item = values[1];
var itemContainer = itemsControl.ItemContainerGenerator.ContainerFromItem(item);
// It may not yet be in the collection...
if (itemContainer == null)
{
return Binding.DoNothing;
}
var itemIndex = itemsControl.ItemContainerGenerator.IndexFromContainer(itemContainer);
return itemIndex;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return targetTypes.Select(t => Binding.DoNothing).ToArray();
}
}
'ItemsControl.Items.IndexOf(values [1])''? –
我相信'Items'集合包含UIElements而不是綁定的實際項目。 – Reddog
不,它不會,如果是這樣的話,ItemContainerGenerator上的那些方法將是多餘的。 –
- 1. 是否有可能從剛剛從列表中刪除的項目中獲取選擇列表的名稱
- 2. indexeddb - 是否有可能從索引獲得位置i的項目
- 3. 從包含列表可能重複獲取特定項目的索引
- 4. 獲取Redis列表項目索引
- 5. 按索引獲取列表項目
- 6. LINQ - 從特定索引開始從列表中獲取項目
- 7. 從綁定列表中獲取已刪除項目的索引
- 8. 從ExpandableListView中獲取項目的索引
- 9. 從按鈕中獲取列表框項目索引單擊
- 10. 如何從列表框中獲取項目索引
- 11. 從列表列表中獲取索引
- 12. 從索引中獲取項目Laravel 5
- 13. 是否有可能從整個Spotify庫中獲取曲目列表?
- 14. 從列表中獲取索引元素是否線程安全?
- 15. 是否有可能從數組中的一組項目中獲取rangeOfString?
- 16. 是否有可能從不同的目錄中獲取模板?
- 17. 從post-> ID獲取條款列表,是否有可能?
- 18. php - 從foreach獲取項目的索引?
- 19. 如何獲取C#中列表中新增項目的索引?
- 20. 是否有可能使用jQuery獲取HTML元素數組項的索引?
- 21. 是否可以從Windows搜索項目獲取Outlook MailItem EntryIDUrl
- 22. 是否有可能從給定特定行的全文索引中獲取相關單詞列表?
- 23. 是否有可能從IOS項目中的cpp文件獲取文件路徑
- 24. 只有當字符串項目索引==列表項目索引
- 25. 獲取數組列表中項目的索引;
- 26. 獲取列表視圖中點擊項目的索引
- 27. 通過列表和數組中的索引獲取struct項目
- 28. 如何獲取列表框中某個項目的索引
- 29. 如何獲取列表框中所選項目的索引?
- 30. 試圖獲取列表中某個項目的索引
幾乎任何事情都可能在WPF中 - 使用正確的XAML /代碼隱藏組合 –