Providing ImageSource Property on UserControl for Use in Blend

I have a custom control that exposes a property of type ImageSource. I want to show this property in Blend so I can change it in Blend instead of specifying an image in code.

Depending on what I googled, I added a dependency property and specified the appropriate attributes to open the property in Blend.

I see it there and edit it (like a textbox). I want to do this with a dropdown list of available image resources and a browse button to load another image. In other words, I want it to behave like the Source property of the Image control.

edit Just like aside, I noticed that displaying the alignment or margin properties behaves as expected, it just seems like the images are not working. I'm really stuck on this and would appreciate your help!

My current code looks like this:

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;
    }
}

      

0


source to share


1 answer


I'm working on almost this problem (minus Blend, plus using a property in a ControlTemplate in XAML.)

In my case, I started working by changing ImageSource

to BitmapSource

. ImageSource is abstract, BitmapSource extends ImageSource.

However, it is not. The Image.Source type is an ImageSource. Is this abstract, it seems that I should be using a DependencyProperty of type ImageSource.

So, for my own case, I have a job with BitmapSource

, but I'm still investigating.



EDIT: I hope you don't mind answering almost a year after you ask, +/- 12 hours.;)

EDIT2: George, I also got 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); }
}

      

+1


source







All Articles