Xaml 如何在样式(UWP)中绑定DependencyProperty?

Xaml 如何在样式(UWP)中绑定DependencyProperty?,xaml,uwp,Xaml,Uwp,我想做一个自定义的用户控件,它实际上只是一个图像按钮。 以下是我的项目: 以下是ImageButton.cs的代码: 以下是ImageButton.xaml的代码: 但是再也没有用了。 我的程序出了什么问题?你能帮帮我吗?你不必为了显示图像作为背景而扩展按钮。您可以像这样直接使用ImageBrush <Button> <Button.Background> <ImageBrush ImageSource="Assets/Wide310x1

我想做一个自定义的用户控件,它实际上只是一个图像按钮。 以下是我的项目:

以下是ImageButton.cs的代码:

以下是ImageButton.xaml的代码:

但是再也没有用了。
我的程序出了什么问题?你能帮帮我吗?

你不必为了显示图像作为背景而扩展按钮。您可以像这样直接使用ImageBrush

<Button>
    <Button.Background>
        <ImageBrush ImageSource="Assets/Wide310x150Logo.png" />
    </Button.Background>
</Button>
二,。将基类更改为Button,并将您自己的依赖属性添加到该类中

    public sealed class ImageButton : Button

    public ImageSource BackgroundImage
    {
        get { return (ImageSource)GetValue(BackgroundImageProperty); }
        set { SetValue(BackgroundImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BackgroundImageProperty =
        DependencyProperty.Register("BackgroundImage", typeof(ImageSource), typeof(ImageButton), null);
三,。提供您自己的样式并将其放在Generic.xaml中。注意你 将需要使用TemplateBinding从您的 绑定目标的依赖项属性

        <Style TargetType="local:ImageButton">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:ImageButton">
                            <Grid>
                                <Image Stretch="UniformToFill" Source="{TemplateBinding BackgroundImage}"></Border>
                                ...
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
就这样。现在,你可以像下面那样使用它

<my:ImageButton Height="100" Width="100" BackgroundImage="Assets/Wide310x150Logo.png" />

类ImageButton:Button声明派生的Button类,而不是UserControl。该类不会神奇地将ResourceDictionary加载到与该类同名的文件中。在VisualStudio项目中创建适当的UserControl,或者查看一下。为什么要创建自己的按钮派生版本?正如@Clemens所说的那样,这样做是行不通的。为什么不编辑按钮控件的样式呢?您只需要更改按钮的外观即可。@Clemens Beaucase我想继承按钮的属性,例如前台fontsize等。是否有更好的方法来继承按钮?@AdityaSharma我不仅想更改样式,还想添加一个自定义属性名称BackgroundImage。但是,做ti的最佳方法是什么?你的BackgroundImage属性做什么?这是我使用的最简单的方法。但是,有这么多地方需要使用ImageButton。总是键入代码或复制/粘贴代码非常麻烦。所以我想制作一个usercontrol以使其更方便使用。usercontrol?但是,根据您问题中的代码,您正在创建一个自定义控件。你到底想在这里做什么?我很抱歉我的错误。这是自定义控件,但不是用户控件。是的,它可以工作。但我发现了另一个问题并解决了它。问题是我使用边框显示背景图像,但它不会显示图像。我应该使用图像来显示它。非常感谢你的帮助!
<Button>
    <Button.Background>
        <ImageBrush ImageSource="Assets/Wide310x150Logo.png" />
    </Button.Background>
</Button>
[![enter image description here][1]][1]

Visual Studio will now automatically generate a new folder called
`Themes` with a `ResourceDictionary` file named `Generic.xaml`,
which is where the default style of your `ImageButton` goes.

[![enter image description here][2]][2]

Visual Studio will also generate an `ImageButton` class with the
following code.

    public sealed class ImageButton : Control
    {
        public ImageButton()
        {
            this.DefaultStyleKey = typeof(ImageButton);
        }
    }
    public sealed class ImageButton : Button

    public ImageSource BackgroundImage
    {
        get { return (ImageSource)GetValue(BackgroundImageProperty); }
        set { SetValue(BackgroundImageProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BackgroundImageProperty =
        DependencyProperty.Register("BackgroundImage", typeof(ImageSource), typeof(ImageButton), null);
        <Style TargetType="local:ImageButton">
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:ImageButton">
                            <Grid>
                                <Image Stretch="UniformToFill" Source="{TemplateBinding BackgroundImage}"></Border>
                                ...
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
<my:ImageButton Height="100" Width="100" BackgroundImage="Assets/Wide310x150Logo.png" />