2011-05-03 31 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並將鼠標移動到您感興趣的控件上。

相關問題