从我的自定义控件中的`System.Windows.Controls.Button`复制样式?

时间:2011-05-03 16:58:11

标签: c# wpf wpf-controls custom-controls

确定。我对WPF中的自定义控件非常陌生,所以我需要很多指导。我想像this article那样制作面包屑按钮,但这次是在WPF中 我想我可以弄明白自己如何画出形状......经过艰苦的努力,但现在又有了另一个问题 在绘制背景时,我确实需要使用标准的背景画笔,不加改变System.Windows.Controls.Button

编辑:


请帮帮我......

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPF_Bread_Crumb_Button"
    xmlns:my="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">

    <Style TargetType="{x:Type local:Crumb}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Crumb}">
                    <my:ButtonChrome Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}" Name="Chrome" RenderDefaulted="{TemplateBinding Button.IsDefaulted}" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderPressed="{TemplateBinding ButtonBase.IsPressed}" SnapsToDevicePixels="True">
                        <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Margin="{TemplateBinding Control.Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" />
                    </my:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="UIElement.IsKeyboardFocused" Value="True">
                            <Setter Property="my:ButtonChrome.RenderDefaulted" TargetName="Chrome" Value="True" />
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter Property="my:ButtonChrome.RenderPressed" TargetName="Chrome" Value="True" />
                        </Trigger>
                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="Control.Foreground" Value="#FFADADAD" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

3 个答案:

答案 0 :(得分:3)

在设计器中,选择一个按钮,打开它的属性,右键单击Template标题,然后选择“Extract Value to Resource”,然后选择目标文件。

Extract Template

它会使用您指定的密钥将默认WPF模板复制到目标xaml文件,这通常是覆盖WPF中模板的最佳起点。

答案 1 :(得分:2)

默认样式can be found on MSDN(有一个链接“默认WPF主题”),只需要在样式中找到正确的渐变。

这是默认背景:

    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F3F3F3" Offset="0"/>
        <GradientStop Color="#EBEBEB" Offset="0.5"/>
        <GradientStop Color="#DDDDDD" Offset="0.5"/>
        <GradientStop Color="#CDCDCD" Offset="1"/>
    </LinearGradientBrush>

答案 2 :(得分:0)

还可以选择使用StyleSnooper从WPF控件中提取样式。

对于运行时WPF“调试”,使用Snoop(完全不同但真正令人敬畏的工具)可能会有所帮助。在窥探您正在开发的应用程序时,请尝试按Ctrl-Shift并将鼠标移到您感兴趣的控件上。