How to create WPF dependency property to support image path?

To describe my problem, I created a small application. Usercontrol first:

<UserControl x:Class="WpfApplication10.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="This" DataContext="{Binding ElementName=This}"
    >    
    <Image Source="{Binding Path=MyImageSource}" ></Image>
</UserControl>

second TestApp:

<Window x:Class="WpfApplication10.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication10="clr-namespace:WpfApplication10"
    Title="Window1" Height="300" Width="300">
    <WpfApplication10:UserControl1
        MyImageSource="c:\test.png" >        
    </WpfApplication10:UserControl1>
</Window>

third code behind usercontrol

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace WpfApplication10
{
    public partial class UserControl1 : UserControl
    {
        public static readonly DependencyProperty MyImageSourceProperty =
            DependencyProperty.Register("MyImageSource", 
                 typeof(BitmapImage),typeof(UserControl1),
                 new FrameworkPropertyMetadata((BitmapImage)new BitmapImage(),
                      FrameworkPropertyMetadataOptions.None
                 ));

        public BitmapImage MyImageSource
        {
            get { return (BitmapImage)GetValue(MyImageSourceProperty); }
            set { SetValue(MyImageSourceProperty, value); }
        }


        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

I know that the BitMapImage type (from DP) does not work this way, but I want to know how to implement this function in pure, multi-line mode. I want to achieve the same behavior as the original Image.Source implementation.

well in advance, Scott

+3
source share
3 answers

Under your control, name your image as:

<Image x:Name="someImage" Source="{Binding Path=MyImageSource}" ></Image>

Implement your dependency property as Uri:

public static readonly DependencyProperty MyImageSourceProperty =
    DependencyProperty.Register("MyImageSource", 
        typeof(Uri),typeof(UserControl1),
        new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));

And in OnImageSourceChanged:

private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    UserControl1 userControl = (UserControl1)sender;

    userControl.someImage.Source = new BitmapImage((Uri) e.NewValue);
}

, , Uri.

EDIT: Image ImageSource, Uri ( , , BitmapSource). OnSourceChanged , Uri. , , .

+4

. DP Bitmapimage, XAML .

, . http://www.codeplex.com/wpfconverters

Google WPF-

public object Convert(object value, Type targetType,
                      object parameter, CultureInfo culture)
{
    try
    {
        return new BitmapImage(new Uri((string)value));
    }
    catch 
    {
        return new BitmapImage();
    }
}

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

}

MyImageSource . UserControl.

+1

set the MyImageSource property to a string of type not BitmapImage

public static readonly DependencyProperty MyImageSourceProperty =
        DependencyProperty.Register("MyImageSource", 
             typeof(string),typeof(UserControl1),
new FrameworkPropertyMetadata(OnImageSourcePathChanged));

    public string MyImageSource
    {
        get { return (BitmapImage)GetValue(MyImageSourceProperty); }
        set { SetValue(MyImageSourceProperty, value); }
    }

 private static void OnImageSourcePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((UserControl1)d).MyImageSource.Source = new BitmapImage(new Uri(e.NewValue as string));

    }

using:

<WpfApplication10:UserControl1 MyImageSource="c:\test.png" > </WpfApplication10:UserControl1>
0
source

Source: https://habr.com/ru/post/1722575/


All Articles