Wpf 图像源值-硬编码字符串和绑定的组合
我的xaml中有一个图像:Wpf 图像源值-硬编码字符串和绑定的组合,wpf,image,xaml,data-binding,Wpf,Image,Xaml,Data Binding,我的xaml中有一个图像: <Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="30" Height="30"></Image> 基本上,.png文件的名称是项目的id 在我的ViewModel中,我有一个表示id的字段,它的名称是myId 如何添加这种类型的源-具有两个硬编码值和一个绑定值?将图像源与id绑定,并应用转换器创建图像路径 xaml将是: ente<Image Source="{Bi
<Image Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Width="30" Height="30"></Image>
基本上,.png文件的名称是项目的id
在我的ViewModel中,我有一个表示id的字段,它的名称是myId
如何添加这种类型的源-具有两个硬编码值和一个绑定值?将图像源与id绑定,并应用转换器创建图像路径 xaml将是:
ente<Image Source="{Binding Path=myId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource ConvImageSource}}"/>
希望这会有所帮助。通过另一个返回图像路径的视图模型属性,或者通过适当的绑定转换器,将您的ID转换为图像路径字符串、URI或ImageSource。我收到错误:{“无法将'System.Windows.Media.ImageSourceConverter'类型的对象强制转换为'System.Windows.Data.IValueConverter'。”在
转换回
处设置断点;如果您在这里获得异常,那么您需要根据需求实现ConvertBack
。Convert和ConvertBack甚至都没有命中。
ente<Image Source="{Binding Path=myId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource ConvImageSource}}"/>
<Window.Resources>
<local:ImageSourceConverter x:Key="ConvImageSource"/>
</Window.Resources>
public class ImageSourceConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String myId = System.Convert.ToString(value);
String imagePath = SPECIFY_IMAGE_PATH;
String imageExtn = SPECIFY_IMAGE_Extn;
// Create the image source
String imageSource = String.Concat(imagePath, myId, imageExtn);
return imageSource;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
#endregion
}