简体   繁体   English

WPF自定义按钮和DependencyObject

[英]WPF Custom Button And DependencyObject

I am trying to create a custom Button with the property ButtonTheme . 我正在尝试使用属性ButtonTheme创建一个自定义Button This should be responsible for the Background of the Button. 这应该负责按钮的背景。 I want to set this in the XAML-code. 我想在XAML代码中进行设置。

My Custom Button 我的自定义按钮

public enum Theme
{
    Black,
    White
}

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
    {
        get
        {
            return (Theme)GetValue(ButtonThemeProperty);
        }
        set
        {
            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;
        }
        else
        {
            brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/BugReport;component/Images/bug_black.png"));
            control.Background = brush;
        }
    }

    protected override void OnClick()
    { 
        base.OnClick();
        BugWindow bug = new BugWindow(Email, Version, Product);
        bug.ShowDialog();
    }

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

How I Want To Call It In XAML 我想如何在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". 问题 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. 但这无法满足我在Button中需要的东西。 What am I missing here? 我在这里想念什么?

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 ). 您在DependencyProperty.Register调用中有无效参数,即第三个参数Type ownerType应该指示正在注册依赖项属性的类型(所有者类型),而不是其值类型(由第二个参数Type propertyType指定) 。 Also, the owner type should derive from DependencyObject . 另外,所有者类型应从DependencyObject派生。 Now you pass typeof(Theme) as the owner type (which it isn't), hence you get the error. 现在,您将typeof(Theme)传递为所有者类型(不是),因此会出现错误。

What you should do is pass typeof(BugButton) as the third parameter ( BugButton is the actual registering type) instead: 您应该做的是传递typeof(BugButton)作为第三个参数( BugButton是实际的注册类型):

public static readonly DependencyProperty ButtonThemeProperty =
    DependencyProperty.Register(
        "ButtonTheme",
        typeof(Theme),
        typeof(BugButton),
        new PropertyMetadata(Theme.Black, new PropertyChangedCallback(ValueChanged)));

Also, the call to ValueChanged in the ButtonTheme setter is redundant. 同样,对ButtonTheme设置程序中的ValueChanged的调用是多余的。 It will be called by the framework upon calling SetValue(...) if the value has actually changed. 如果值实际上已更改,则框架将在调用SetValue(...)时调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM