在我使用mvvm的Silverlight5应用程序中,需要实现下面的一个。 我有包含图像的图像文件夹。如何获取整个图像路径并将其分配给超链接按钮。
<HyperlinkButton Content="Preview" NavigateUri="{Binding image_value}"
TargetName="_blank" />
但是我需要给出如下的路径,
("./Images/{0}", String_Value)
帮助我实现这个目标..
答案 0 :(得分:2)
您应该使用Converter
附加这些字符串:
public class ImagePathConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parametr, CultureInfo culture)
{
var imgPath = "./Images/{0}";
return string.Format(imgPath, value);
}
public object ConvertBack(object value, Type targetType, object parametr, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
xaml
:
<Page.Resources>
<converters:ImagePathConverter x:Key="imagePathConverter"/>
...
</Page.Resources>
...
<HyperlinkButton Content="Preview" NavigateUri="{Binding image_value, Converter={StaticResource imagePathConverter}}" TargetName="_blank" />
* 我建议不要对路径进行硬编码,而是将其放入资源文件中,这样可以提供更大的灵活性。
答案 1 :(得分:0)
Hai我找到了解决方案..
<HyperlinkButton Content="Preview" Margin="2" NavigateUri="{Binding String_Value,StringFormat='http://localhost:23411/ClientBin/Images/{0}'}" VerticalAlignment="Center" TargetName="_blank" />
我把它放在我的代码中并且工作得很好......