【问题标题】:binding images in listbox在列表框中绑定图像
【发布时间】:2013-12-03 03:09:41
【问题描述】:

我有一个从数据表填充的列表框。我希望每个项目在列表框中都有一个特定的图像,但我想根据每个项目的 id 设置图像。 例如,我有:

产品

橙色

苹果

ID

1

2

图片命名为:Item.1.pngItem.2.png

所以,在我有苹果的列表框中,我将在它旁边有一个名为:Item.2.png 的图像。

我的问题是我不知道如何进行条件绑定。我不想在我的模板上有数百行为每个项目执行此操作。我需要使用条件来执行此操作,例如:if(product.id==1), Image.Source=Item.1.png。 在 wpf 中有没有办法做到这一点?

【问题讨论】:

    标签: c# wpf binding listbox


    【解决方案1】:

    听起来你需要一个IdToImageConverter,它会根据Id 属性的值来决定应该显示哪个Image。像这样的东西应该可以解决问题:

    [ValueConversion(typeof(int), typeof(ImageSource))]
    public class IdToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.GetType() != typeof(int) || targetType != typeof(ImageSource)) return false;
            int id = (int)value;
            if (id < 0) return DependencyProperty.UnsetValue;
            string imageName = string.Empty;
            switch (id)
            {
                case 1: imageName = "Item.1.png"; break;
                case 2: imageName = "Item.2.png"; break;
            }
            if (imageName.IsEmpty()) return null;
            return string.Format("/AppName;component/ImageFolderName/{0}", imageName);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return DependencyProperty.UnsetValue;
        }
    

    然后你可以在你的 ListBox.ItemTemplate 中使用它,如下所示:

    <YourConvertersXmlNamespacePrefix:IdToImageConverter x:Key="IdToImageConverter" />
    ...
    <ListBox ItemsSource="{Binding Items}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Id, Converter={StaticResource 
    IdToImageConverter}}" />
                    <TextBlock Text="{Binding Name}" Margin="5,0,0,0" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

      【解决方案2】:

      据我了解,列表框中的每个对象都有一个 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();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-24
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-21
        • 2019-08-17
        • 1970-01-01
        相关资源
        最近更新 更多