繁体   English   中英

WPF在图像源中添加文件扩展名

[英]WPF Add file extension in image source

我正在Blend中进行WPF / XAML项目,我有2个项目:

  1. 具有“ TSM”值的TextBlockDesign

  2. 图片

我需要图像的图像源为TSM.png

我需要从textbox获取该TSM

因此,图像源应为[TextBox].png

我该怎么做?

您可能可以通过使用绑定和转换器来实现。

绑定从文本框中获取值,并使用转换器对其进行转换。

尝试这个:

<Control.Resources>
    <my:ImageSourceConverter x:Key="sourceConv" />
</Control.Resources>
<TextBox x:Name="txtTSM" />
...
<Image Source="{Binding Path=Text,ElementName=txtTSM,Converter={StaticResource sourceConv}}" />

转换器类可能看起来像这样。 请注意,您必须自己实现字符串到ImageSource的转换。 我目前无法使用VS,因此其中可能包含一些小错误。

public class ImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // convert string to image source
        String filename = string.Format("{0}.{1}",(string)value,"png");
        return new ImageSource(filename);
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM