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

发布于 2024-11-04 11:25:15 字数 2738 浏览 1 评论 0原文

好的。我对在 WPF 中制作自定义控件完全陌生,所以我需要很多指导。我想制作一个像 本文 中那样的面包屑按钮,但这次是在 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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲喜皆因你 2024-11-11 11:25:15

在设计器中,选择一个按钮,打开其属性,右键单击模板标题并选择“将值提取到资源”,然后选择目标文件。

Extract Template

它将使用您指定的密钥将默认的 WPF 模板复制到目标 xaml 文件,这通常是最好的开始指向 WPF 中的重写模板。

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.

Extract Template

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.

萌逼全场 2024-11-11 11:25:15

默认样式可以在MSDN上找到(有一个链接“Default 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>

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>
眉目亦如画i 2024-11-11 11:25:15

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文