Dependency property not working, trying to set through style setter?

The standard form for a dependency property is (i've added in your information).

Up vote 1 down vote favorite share g+ share fb share tw.

I am trying to set up a custom style for my newly made usercontrol, however I am getting the error : "Cannot convert the value in attribute 'Property' to object of type 'System.Windows. DependencyProperty'. " I thought I had set up Dependency properties but it seemed this was not the case, so I did some research and added: public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.

Register("ImageSource", typeof(BitmapSource), typeof(Image)); to make this: -- MyButton.Xaml. Cs -- namespace Client. Usercontrols { public partial class MyButton : UserControl { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.

Register("ImageSource", typeof(BitmapSource), typeof(Image)); public MyButton() { InitializeComponent(); } public event RoutedEventHandler Click; void onButtonClick(object sender, RoutedEventArgs e) { if (this. Click! = null) this.

Click(this, e); } BitmapSource _imageSource; public BitmapSource ImageSource { get { return _imageSource; } set { _imageSource = value; tehImage. Source = _imageSource; } } } } This unfortunately does not work. I also tried this: public BitmapSource ImageSource { get { return (BitmapSource)GetValue(MyButton.

ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } But that did not work and the image was not shown and generated the same error as mentioned previously anyway. Any ideas? Regards Kohan.

-- MyButton. Xaml -- -- MYButton Style -- wpf xaml styles dependency-properties link|improve this question edited Oct 21 '09 at 11:52 asked Oct 20 '09 at 9:44Kohan1,5061729 94% accept rate.

The standard form for a dependency property is (i've added in your information): public BitmapSource ImageSource { get { return (BitmapSource)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty ImageSourceProperty = DependencyProperty. Register("ImageSource", typeof(BitmapSource), typeof(MyButton), new UIPropertyMetadata(null)); it seems like your also trying to pass through the dependency property to the ImageSource of the object called "tehImage".

You can set this up to automatically update using the PropertyChangedCallback... this means that whenever the property is updated, this will call the update automatically. Thus the property code becomes: public BitmapSource ImageSource { get { return (BitmapSource)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } // Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc... public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.

Register("ImageSource", typeof(BitmapSource), typeof(MyButton), new UIPropertyMetadata(null, ImageSource_PropertyChanged)); private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { ((MyButton)source).tehImage. ImageSource = (ImageSource)e. NewValue } Hopefully with the correctly registered dependency property, this will help you narrow down the issue (or even fix it).

Thanks, I think I am almost there. I had to rework ImageSource_PropertyChanged to get it to build and maybe this is where it's going wrong? But my style (which I have now added above to see) is not applying to my button when it is set to disabled.

Any ideas? Private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { ((MyButton)source).tehImage. Source = (ImageSource)e.

NewValue; } – Kohan Oct 21 '09 at 11:57.

Biggest problem I see is that you're registering the property as owned by Image rather than by your UserControl. Change to: public static readonly DependencyProperty ImageSourceProperty = DependencyProperty. Register("ImageSource", typeof(BitmapSource), typeof(MyButton)); If that doesn't work, will need to see your XAML.

That seems to have stopped the error from occurring in my style now, however it now stops my usercontrol from working, it has disappeared from all the places it was used. I have added the usercontrol's Xaml. – Kohan Oct 20 '09 at 10:18.

Set the DataContext for your UserControl: public MyButton() { InitializeComponent(); DataContext = this; } Alternatively, if you can't do that (since the DataContext is set to another object, for example), you can do this in your XAML.

The correct way of implementing a source for an Image in a user control in my opinion is not BitmapSouce. The easiest and best way (according to me again) is using Uri. Change your dependency property to this (while also defining a change callback event): ImageSourceProperty = DependencyProperty.

Register( "ImageSource", typeof (Uri), typeof (MyButton), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged))); and the property to this: public Uri ImageSource { get { return (Uri)GetValue(ImageSourceProperty); } set { SetValue(ImageSourceProperty, value); } } Where your call back is like this: private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { MyButton hsb = (MyButton)sender; Image image = hsb. TehImage; image. Source = new BitmapImage((Uri) e.

NewValue); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions