WPF将Binded值从整数更改为图像

时间:2013-04-11 15:18:30

标签: wpf vb.net xaml bitmapimage ivalueconverter

我正在尝试将绑定到ListView的ListView加载到带有项列表的数据库中。在ListView中,我显示两个列,“项目”和“状态”。我能够带来值,但现在我想将Status中的值替换为Image。

示例:

1 = images/green.png
2 = images/red.png
3 = images/orange.png

我想在列表中显示图像,以便在用户导航时,他们会自动查看所有图像。我在另一个问题中找到了类似的东西,但是内置了image标签,我在ListView中无法做到这一点。 WPF Convert Integer to Image without DB Binding

感谢您的帮助。

修改

Partial Class ImgConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object
        Dim imageIndex As Integer
        If Integer.TryParse(value.ToString(), imageIndex) Then
            Select Case imageIndex
                Case 1
                    Return BitmapSource = "Images/green.png"
                Case 2
                    Return BitmapSource = "Images/red.png"
            End Select
        End If
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object
        Throw New NotImplementedException()
    End Function

End Class

使用此代码我得到一个IntelliSense错误BitmapSource和Implements IvalueConverter说我需要实现一个ConvertBack,我做了但它仍然让我感到烦恼。

编辑#3

确定BITMAPSOURCE错误是BC我没有宣布变量。解决了。​​

IValueConverter错误说: 错误2类'ImgConverter'必须实现'Function ConvertBack(value As Object,targetType As Type,参数As Object,culture As Globalization.CultureInfo)As''作为接口'System.Windows.Data.IValueConverter'。 ... \ Config(ABM,20)\ RangoPage.xaml.vb 14 Cogent

2 个答案:

答案 0 :(得分:3)

使用IValueConverter,

将整数作为参数,并返回BitmapSource

<ListView ItemsSource="{Binding Collection}">
   <ListView.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <Textblock Text="{Binding Item}" />
            <Image Source="{Binding Status, Converter={StaticResource ValueConverter}}" />
         </StackPanel>
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

public class IntToImageConverter : IValueConverter
{
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int imageIndex;
            if(int.TryParse(value.ToString(), out imageIndex))
            {
               switch(imageIndex)
               {
                 case 1:
                  return new ImageSource("images/red.png")

                 etc...
               }
            }

            return null;
        }

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

答案 1 :(得分:0)

如果您需要在 UWP 应用中使用它,则可以使用。

C#

 public sealed class IntStateToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                var imagePath = "ms-appx:///Assets/Buttons/";
                DeviceNodeTechStateEnum state = value.ObjectToEnums<DeviceNodeTechStateEnum>();
                switch (state)
                {
                    case DeviceNodeTechStateEnum.OK:
                        imagePath += "correct.png";
                        break;
                    case DeviceNodeTechStateEnum.BAD:
                        imagePath += "incorrect.png";
                        break;
                    default:
                        imagePath += "unknown.png";
                        break;
                }

                Uri imageUri = new Uri(imagePath, UriKind.Absolute);
                BitmapImage imageBitmap = new BitmapImage(imageUri);
                return imageBitmap;
            }
            catch (Exception)
            {
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return !(value is bool && (bool)value);
        }
    }

ms-appx:///Assets/Buttons/是用于保存图像的项目文件夹。

UserControl的XAML

   <Image  Source="{Binding State, Converter={StaticResource intStateToImageConverter}}" Width="20" Height="20" ></Image>

State在哪里,是该类的字段,类型为DeviceNodeTechStateEnum

APP的XAML

<Application.Resources>
  <ResourceDictionary>
    <common:IntStateToImageConverter x:Key="intStateToImageConverter" />

C#枚举

public enum DeviceNodeTechStateEnum
{
        Undefined = 1,
        OK = 2,
        BAD = 3,
}

object转换为enums的方法。

public static class Extensions
{
        public static T ObjectToEnums<T>(this object o)
        {
            T enumVal = (T)Enum.Parse(typeof(T), o.ToString());
            return enumVal;
        }
}