ImageSource using filename from database in WPF

前端 未结 1 351
醉梦人生
醉梦人生 2021-01-16 07:23

I have a Rectangle in WPF, I can set it\'s Fill by using . This is my XAML for Rectangle:

&         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 08:20

    You should use a Binding Converter, which may look like this:

    public class ImageSourceConverter : IValueConverter
    {
        public object Convert(
            object value, Type targetType, object parameter, CultureInfo culture)
        {
            return new BitmapImage(new Uri(
                string.Format("pack://application:,,,/Images/{0}.jpg", value)));
        }
    
        public object ConvertBack(
            object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    Declare the converter as XAML resource like this:

    
        
    
    

    und use it in your Binding:

    
    

    More about using converters can be found here: https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-convert-bound-data

    0 讨论(0)
提交回复
热议问题