Tom Unger Tom Unger - 2 years ago 106
C# Question

WPF Custom Button And DependencyObject

I am trying to create a custom

with the property
This should be responsible for the Background of the Button. I want to set this in the XAML-code.

My Custom Button

public enum Theme

public class BugButton : Button
public string Email { get; set; }
public string Version { get; set; }
public Produkte Product { get; set; }
public static readonly DependencyProperty ButtonThemeProperty = DependencyProperty.Register("ButtonTheme", typeof(Theme), typeof(Theme), new PropertyMetadata(Theme.Black, new PropertyChangedCallback(ValueChanged)));
public Theme ButtonTheme
return (Theme)GetValue(ButtonThemeProperty);
SetValue(ButtonThemeProperty, value);
ValueChanged(this, new DependencyPropertyChangedEventArgs(ButtonThemeProperty, value, value));

public BugButton()


private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
var control = d as BugButton;

var brush = new ImageBrush();
if ((Theme)e.NewValue == Theme.White)
brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/BugReport;component/Images/bug.png"));
control.Background = brush;
brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/BugReport;component/Images/bug_black.png"));
control.Background = brush;

protected override void OnClick()
BugWindow bug = new BugWindow(Email, Version, Product);

public void SetParameters(string aEmail, string aVersion, Produkte aProduct)
Email = aEmail;
Version = aVersion;
Product = aProduct;

How I Want To Call It In XAML

<BugReport:BugButton x:Name="B_bug" ButtonTheme="White" Margin="0,8,8,0" Style="{StaticResource HeaderButtonHoverMakeover}" Foreground="White" HorizontalAlignment="Right" Width="25" Height="25" VerticalAlignment="Top" Background="White"/>

The Problem
The type "Theme" must be derived from "DependencyObject".

So I guess it has to look like this:

public class BugButton : DependencyObject

But this does not work out with the things I need from the Button. What am I missing here?

Answer Source

You've got invalid argument in your DependencyProperty.Register call, namely the third parameter Type ownerType should indicate the type that is registering the dependency property (the owner type) and not its value type (which is specified by the second parameter Type propertyType). Also, the owner type should derive from DependencyObject. Now you pass typeof(Theme) as the owner type (which it isn't), hence you get the error.

What you should do is pass typeof(BugButton) as the third parameter (BugButton is the actual registering type) instead:

public static readonly DependencyProperty ButtonThemeProperty =
        new PropertyMetadata(Theme.Black, new PropertyChangedCallback(ValueChanged)));

Also, the call to ValueChanged in the ButtonTheme setter is redundant. It will be called by the framework upon calling SetValue(...) if the value has actually changed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download