2011-05-03 214 views
0

好的。我在WPF中制作自定义控件是全新的,所以我需要很多指导。我想制作一个面包屑按钮,如this article,但这次是在WPF中。
我想我可以弄清楚自己如何画出形状......经过艰苦的工作,但现在我又有了一个担忧。
我真的需要在绘制我的背景时获取并使用标准的背景画笔,不会改变System.Windows.Controls.Button在我的自定义控件中从`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> 

回答

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

这只会导致我的Visual Studio崩溃...... :( – Vercas 2011-05-03 17:13:29

+0

您需要获取所有资源渐变引用以及-_- – 2011-05-03 17:41:15

+0

@Vercas:我有一个有点误导性的链接,因为在其父网站上找到默认样式,请检查答案中的更新链接。 – 2011-05-03 17:57:25

3

在设计,选择一个按钮,打开它的属性,右键单击模板头,并选择“获取价值,以资源”,然后选择目标文件。

Extract Template

它会默认WPF模板复制到你指定的关键目标XAML文件,这通常是最好的起点,在WPF覆盖模板。

+0

这看起来好多了!我正在尝试! – Vercas 2011-05-03 17:22:20

+0

这是我的理解,我吮吸WPF – Vercas 2011-05-03 17:33:22

+0

@vercas:这与你在MSDN上得到的没有什么不同...... – 2011-05-03 17:41:48

0

还有一个选项可以使用StyleSnooper从WPF控件中提取样式。

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