据我了解,列表框中的每个对象都有一个 ID 属性,您希望图像成为项目。ID.png。
您可以在绑定中使用转换器来执行此操作。所以在你的列表框模板中,你可以有这样的东西:
// ... Listbox template
<Image Source={Binding pathToItemID, Converter={StaticResource MyConverter}/>
// ... Remaining ListBox template
您需要将转换器添加到 UserControl 的资源中:
<UserControl.Resources>
<xmlnsPointingToConverter:MyConverter x:Key="MyConverter"/>
</UserControl.Resources>
然后添加一个实现IValueConverter的MyConverter类:
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.Format("item.{0}.png", value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}