To describe my problem i have created a small application. At first the Usercontrol:
<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 the 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 the code behind the 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 Type BitMapImage (of the DP) doesnt work in this way, but i want to know how to implement this feature in a clean, typesave way. I want to reach the same behaviour like the original Image.Source implementation.
thanks in advance, Scott