George Sealy
George Sealy

Reputation: 966

Exposing an ImageSource property in a UserControl for use in Blend

I have a user control that exposes a property of type ImageSource. I want to expose this property in Blend so that I can edit it in Blend, rather than specifying the image in code.

Based on what I've Googled, I've added a dependency property, and specified appropriate attributes to expose the property in Blend.

I can see it there, and edit it (as a text field). What I want to do is have a drop down list of available image resources, and a browse button for loading up another image. In other words I want it to behave like the 'Source' property of the 'Image' control.

edit Just as an aside, I've noticed that exposing Alignment or Margin properties behaves as expected, it just seems to be Images that don't work. I'm really stuck on this one and would appreciate help!

My current code looks like:

public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(SmartButton));

[Category("Appearance")]
[Bindable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public ImageSource ImageSource
{
    get { return (ImageSource)GetValue(ImageSourceProperty); }
    set
    {
        SetValue(ImageSourceProperty, value);
        this.BackgroundImage.Source = value;
    }
}

Upvotes: 0

Views: 3428

Answers (1)

JMD
JMD

Reputation: 7377

I have been working on almost exactly this problem (minus Blend, plus using the property in a ControlTemplate in XAML.)

In my case I have gotten it to work by changing ImageSource to BitmapSource. ImageSource is abstract, BitmapSource extends ImageSource.

However, something doesn't feel right about this. The type of Image.Source is ImageSource. Whether or not it's abstract, it seems like I should be able to use a DependencyProperty of type ImageSource.

So, for my own case, I've got it working with BitmapSource but I'm still investigating.

EDIT: Hopefully you don't mind an answer almost a year after you asked, +/- 12 hours. ;)

EDIT2: George, I did also get this to work for me with ImageSource using:

public static readonly DependencyProperty SourceProperty =
    Image.SourceProperty.AddOwner(typeof(MyCustomButton));
public ImageSource Source
{
    get { return (ImageSource)GetValue(SourceProperty); }
    set { SetValue(SourceProperty, value); }
}

Upvotes: 1

Related Questions