C# 如何设计WPF依赖属性以支持图像路径?

C# 如何设计WPF依赖属性以支持图像路径?,c#,.net,wpf,user-controls,dependency-properties,C#,.net,Wpf,User Controls,Dependency Properties,为了描述我的问题,我创建了一个小应用程序。 首先,用户控件: <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="{Bi

为了描述我的问题,我创建了一个小应用程序。 首先,用户控件:

<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>
我知道(DP的)类型BitMapImage不能以这种方式工作,但我想知道如何以干净、类型保存的方式实现此功能。 我希望达到与原始映像相同的行为。源代码实现


提前感谢Scott,您需要创建一个转换器。由于DP定义为Bitmapimage,并且在XAML中,您将其绑定到字符串

也许你可以在这里找到合适的转换器

或者尝试谷歌“WPF转换器”

}


并将MyImageSource设置为字符串类型。然后可以在用户控件定义中使用转换器。

在控件中,将图像命名为:

<Image x:Name="someImage" Source="{Binding Path=MyImageSource}" ></Image>
在OnImageSource中,更改了:

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

    userControl.someImage.Source = new BitmapImage((Uri) e.NewValue);
}
通过这种方式,您可以为依赖项属性提供字符串路径,该属性将自动转换为Uri

编辑:
在Image中,source属性被实现为ImageSource而不是Uri(一个抽象类,您的实现,即BitmapSource,也从中派生)。它还实现了OnSourceChanged事件,就像我为Uri实现的一样。结论是,如果要设置图像的源,必须使用更改事件。

使字符串类型的MyImageSource属性不是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));

    }
用法:



是的,这会起作用。但我真正的问题是:Image.Source是如何实现的。这个属性以我想要的方式工作。也许这个链接会有用,我更喜欢评论而不是你的答案。TypeConverters是一种通过字符串表示将值设置为非字符串属性的方法。答案描述的是IValueConverter,在本例中可能有效,但与image.source实现不同。嗨,Simpzon,你知道image.source是如何实现的吗?嗨,Scott,我必须承认我不太清楚,但是当从字符串初始化非字符串属性时,通常会有一些TypeConverter执行此转换(除非绑定中定义了IValueConverter)。TypeConverter要么定义为属性上的属性,要么定义为属性的类型(例如在URI中)。这是一个纯代码的答案。请回答并提供更多关于此代码如何回答问题的上下文。
<Image x:Name="someImage" Source="{Binding Path=MyImageSource}" ></Image>
public static readonly DependencyProperty MyImageSourceProperty =
    DependencyProperty.Register("MyImageSource", 
        typeof(Uri),typeof(UserControl1),
        new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));
private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
    UserControl1 userControl = (UserControl1)sender;

    userControl.someImage.Source = new BitmapImage((Uri) e.NewValue);
}
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));

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