开发者

Copy styles from `System.Windows.Controls.Button` in my custom control?

Ok. I am totally new to making custom controls in WPF so I need MUCH guidance. I want to make a breadcrumb button like in this article, but this time in WPF.

I think I can figure out myself how to draw the shapes... After much hard work but now I have another concern.

I literally NEED to get and use the background brush of a standard, unaltered System.Windows.Controls.Button when drawing my background!

Edit:


Please help me out...

<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 ContentCon开发者_运维问答trol.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>


In the designer, select a button, open it's properties, right click on the Template header and chose "Extract Value to Resource", then chose the destination file.

Copy styles from `System.Windows.Controls.Button` in my custom control?

It will copy the default WPF Template to the destination xaml file with the key you indicated, this is usually the best starting point to overriding templates in WPF.


The default style can be found on MSDN (there is a link "Default WPF Themes"), just need to find the right gradient in the style.

This is the default background:

    <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>


There's also the option to use StyleSnooper to extract a style from a WPF control.

For runtime WPF "debugging", using Snoop (completely different, but genuinely awesome tool) might be helpful. When snooping an application you're developing, try pressing Ctrl-Shift and moving the mouse over the controls you're interested in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜