从自定义控件中的“System.Windows.Controls.Button”复制样式?
好的。我对在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在设计器中,选择一个按钮,打开其属性,右键单击模板标题并选择“将值提取到资源”,然后选择目标文件。
它将使用您指定的密钥将默认的 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.
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.
默认样式可以在MSDN上找到(有一个链接“Default 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:
还可以选择使用 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.