ImageSource using filename from database in WPF

前端 未结 1 350
醉梦人生
醉梦人生 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:

    <Window.Resources>
        <local:ImageSourceConverter x:Key="ImageSourceConverter" />
    </Window.Resources>
    

    und use it in your Binding:

    <ImageBrush ImageSource="{Binding Path=itemNumber,
                                      Converter={StaticResource ImageSourceConverter}}"/>
    

    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)
提交回复
热议问题