C# 如何获得';默认按钮外观';在从按钮派生的CustomControl中?

C# 如何获得';默认按钮外观';在从按钮派生的CustomControl中?,c#,wpf,button,custom-controls,C#,Wpf,Button,Custom Controls,刚刚尝试在的帮助下为按钮编写自己的自定义控件 但是按钮控件除了图像和文本之外是空的 如果没有指定其他内容,是否有可能使用默认行为 我不想重新编写一个完整的按钮,如果已经有一个几乎适合存在。只是想添加一个ImageSource和一个文本以及一些(而不是全部)样式 这是我得到的,也许我只是犯了一些错误 类别定义: public class MyImageButton : Button { static MyImageButton() { DefaultStyleKey

刚刚尝试在的帮助下为按钮编写自己的自定义控件

但是按钮控件除了图像和文本之外是空的

如果没有指定其他内容,是否有可能使用默认行为

我不想重新编写一个完整的按钮,如果已经有一个几乎适合存在。只是想添加一个ImageSource和一个文本以及一些(而不是全部)样式

这是我得到的,也许我只是犯了一些错误

类别定义:

public class MyImageButton : Button
{
    static MyImageButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyImageButton), new FrameworkPropertyMetadata(typeof(MyImageButton)));
    }

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

    public static readonly DependencyProperty ImageSourceProperty =
        DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(MyImageButton), new UIPropertyMetadata(null));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MyImageButton), new UIPropertyMetadata(null));
}
从generic.xaml

<Style TargetType="{x:Type local:MyImageButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyImageButton}">
                <StackPanel Height="Auto" Orientation="Horizontal">
                    <Image Source="{TemplateBinding ImageSource}" Width="24" Height="24" Stretch="Fill" />
                    <TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Left" Foreground="Black" FontWeight="Bold"  Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

最后是主窗口xaml中的用法

<ctrl:MyImageButton ImageSource="SomeImage.png" Text="BlaBlubb" Height="100" Width="120" Click="CheckClick" />

这就是您在这里实现的:

<StackPanel Height="Auto" Orientation="Horizontal">
    <Image Source="{TemplateBinding ImageSource}" Width="24" Height="24" Stretch="Fill"/>
    <TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Left" Foreground="Black" FontWeight="Bold"  Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
</StackPanel>

你所说的默认行为是什么意思

一个简单的选择是:

<Style TargetType="{x:Type local:MyImageButton}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MyImageButton}">
                <Button>
                    <StackPanel Height="Auto" Orientation="Horizontal">
                        <Image Source="{TemplateBinding ImageSource}" Width="24" Height="24" Stretch="Fill"/>
                        <TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Left" Foreground="Black" FontWeight="Bold"  Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
                    </StackPanel>
                </Button>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

第二种选择,可能是您的最佳选择:

从按钮派生UserControl,如下所示:

<Button x:class="..."
        x:Name="root">
    <StackPanel Height="Auto" Orientation="Horizontal">
        <Image Source="{Binding Image, ElementName=root}" Width="24" Height="24" Stretch="Fill"/>
        <TextBlock Text="{Binding Text, ElementName=root}" HorizontalAlignment="Left" Foreground="Black" FontWeight="Bold"  Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
    </StackPanel>
</Button>

并在UserControl的codebehind中定义依赖项属性


经验法则:当您的目标是以可重用的方式排列(其他)控件时,请使用UserControls。

如果没有指定其他内容,是否有可能使用默认行为?
-我不明白。如果从
按钮继承,则
已经具有
按钮的行为。你是说外表吗?这是WPF中两个完全不同的概念,我想要的是一个按钮,而不是图像和文本。这里似乎被摧毁了。我在主窗口上看到了两个元素,但没有任何元素看起来像按钮,因为ControlTemplate只指定了您看到的内容—一个带有图像和文本块的StackPanel。按钮的控件模板实际上要复杂得多。请参阅以了解如何提取按钮的ControlTemplate或按钮的ControlTemplate。这正是我想要的!非常感谢你!谢谢你的提示!
<Button x:class="..."
        x:Name="root">
    <StackPanel Height="Auto" Orientation="Horizontal">
        <Image Source="{Binding Image, ElementName=root}" Width="24" Height="24" Stretch="Fill"/>
        <TextBlock Text="{Binding Text, ElementName=root}" HorizontalAlignment="Left" Foreground="Black" FontWeight="Bold"  Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
    </StackPanel>
</Button>