相关文章推荐
奔跑的茄子  ·  C# ...·  2 月前    · 
千杯不醉的绿茶  ·  WPF combobox ...·  2 月前    · 
高大的拐杖  ·  wpf ...·  1 周前    · 
爱运动的小虾米  ·  HTML+jQuery ...·  1 年前    · 
坚强的小熊猫  ·  Pygame入门 2022 ...·  1 年前    · 

wpf listbox item index binding

WPF 中的 ListBox 可以绑定到数据源,在绑定数据源后,每一个 ListBoxItem 都可以有一个索引值。你可以使用 Binding 来实现索引值的绑定。

首先,你需要给 ListBox 设置一个 ItemTemplate,然后在 ItemTemplate 中通过 DataTemplate 设置绑定。

其中,YourDataSource 为你的数据源,YourIndexConverter 为一个转换器,该转换器可以将当前 ListBoxItem 的索引值转换为一个字符串。

public class YourIndexConverter : IValueConverter public object Convert(object value, Type targetType, object parameter, CultureInfo culture) var item = value as YourDataType; var index = YourDataSource.IndexOf(item); return index.ToString();

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    throw new NotImplementedException();

}

  •