2015-11-25 17 views
0

您好我一直在寻找方法来动态地更改图像Uri在xaml和我的研究中遇到了以下answer,这当然给了我希望我真正想要的东西做可能只是可能的。在原始问题中,提问者想要交换图像本身,在我的情况下,我想交换图像所在的目录。是否有可能在xaml中连接imagesourece Uri

因此,当人们看到@Clemens提供的答案时,图像源被绑定到依赖属性,该属性在窗体加载时动态设置。

我想知道的是,是否可以动态设置uri的位置部分(根据@Clemens所倡导的逻辑,然后在实际的绑定语句中简单地添加图像名称,以便它可能是这个样子:

<Image Source="{Binding ImageUri & myImage.png}"/> 

基本上我有一些按钮,我想分配一个默认的图像OG由终端用户来确定的大小为此,该图像将被存储。在应用程序的不同文件夹中(实际上它是一个自定义控件),然后根据引用的答案中的建议设置URI的相关路径位,并且我只需添加图像名称(这将是与按钮大小无关),然后显示正确的图像。

回答

0
  1. MainWindow.xaml.cs:

    namespace MainWindowNamespace 
    { 
        public sealed class ImageConverter : IValueConverter 
        { 
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
          { 
           try 
           { 
            string fullPath = Path.GetFullPath((string)value); 
            return new BitmapImage(new Uri(fullPath)); 
           } 
           catch { return null; } 
          } 
    
          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
          { throw new NotImplementedException(); } 
        } 
    } 
    
  2. MainWindow.xaml:

    <Window 
        xmlns:imgConvert="clr-namespace:MainWindowNamespace"> 
    
        <Window.Resources> 
         <imgConvert:ImageConverter x:Key="ImageConverter" /> 
        </Window.Resources> 
    
        <Image ImageSource="{Binding ImagePath, Converter={StaticResource ImageConverter}}" /> 
    
    </Window> 
    
相关问题