2010-04-28 26 views
2

我創建了一個自定義按鈕,因爲我想圖像和裏面文本如下自定義控件:WPF保持基本樣式在

<Style TargetType="{x:Type Local:ImageButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Local:ImageButton}"> 
        <StackPanel Height="Auto" Orientation="Horizontal"> 
         <Image Margin="0,0,3,0" Source="{TemplateBinding ImageSource}"/> 
         <TextBlock Text="{TemplateBinding Content}" /> 
        </StackPanel> 
      </ControlTemplate> 
      </Setter.Value> 
    </Setter> 
</Style> 

這裏,ImageButton的是從Button類繼承和擁有的ImageSource類作爲依賴屬性。

但我想保持原始按鈕的外觀和感覺。 我該怎麼做? 謝謝。

回答

5

您可以使用Style.BasedOn屬性:

<Style TargetType="{x:Type Local:ImageButton}" BasedOn="{StaticResource {x:Type Button}}"> 
    <!-- ... --> 
</Style> 
+2

我嘗試過,但它不工作。 :( – Archie 2010-04-28 11:25:38

1

好吧,我的第一次嘗試,這是錯誤的。您需要在上面的代碼片段中執行的操作不是覆蓋模板,而是需要覆蓋ContentTemplate。這應該仍然保留原始Button的外觀和風格,但ImageButton中的內容將是文本和圖像。您可能還需要執行gehho建議的操作,以確保它知道其他所有內容都來自隱式按鈕樣式。

+1

我嘗試用「內容」替換「模板」屬性,但它不顯示圖像,但保留原始按鈕的外觀和感覺。ContentTemplate是否意味着其他內容? – Archie 2010-04-28 11:57:25

+0

ContentTemplate只是一種重寫Content Button在大多數方面都起着和Template一樣的作用,除了它替換的區域(它是一個DataTemplate而不是ControlTemplate)。模板取代了整個Button。ContentTemplate只替換Content DataTemplate。 http:// msdn。 microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate%28VS.96%29.aspx – opedog 2010-04-28 12:10:16

+1

它給出了錯誤,因爲無法在類型'ContentPresenter'上找到靜態成員'ImageSourceProperty'。 – Archie 2010-04-28 12:29:41

0

您可以從簡單樣式創建按鈕並對其進行修改。試試這個,我不知道它是否適合你。比較遺憾的是長期的答案(如果它不適合你的情況)

<!-- SimpleStyles.XAML defines a set of control styles which are simplified starting points for creating your own controls --> 

    <!-- Brushes : These are used to define the color for background, foreground, selection, enabled etc of all controls 
If you want to change the color of a control you can just chnage the brush; if you want to add a new shape or change arrangement then also edit the template --> 

    <!-- NormalBrush is used as the Background for SimpleButton, SimpleRepeatButton --> 
    <LinearGradientBrush x:Key="NormalBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#EEE" Offset="0.0"/> 
     <GradientStop Color="#CCC" Offset="1.0"/> 
    </LinearGradientBrush> 
    <LinearGradientBrush x:Key="NormalBorderBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#CCC" Offset="0.0"/> 
     <GradientStop Color="#444" Offset="1.0"/> 
    </LinearGradientBrush> 

    <!-- LightBrush is used for content areas such as Menu, Tab Control background --> 
    <LinearGradientBrush x:Key="LightBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FFF" Offset="0.0"/> 
     <GradientStop Color="#EEE" Offset="1.0"/> 
    </LinearGradientBrush> 

    <!-- MouseOverBrush is used for MouseOver in Button, Radio Button, CheckBox --> 
    <LinearGradientBrush x:Key="MouseOverBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#FFF" Offset="0.0"/> 
     <GradientStop Color="#AAA" Offset="1.0"/> 
    </LinearGradientBrush> 

    <!-- PressedBrush is used for Pressed in Button, Radio Button, CheckBox --> 
    <LinearGradientBrush x:Key="PressedBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#BBB" Offset="0.0"/> 
     <GradientStop Color="#EEE" Offset="0.1"/> 
     <GradientStop Color="#EEE" Offset="0.9"/> 
     <GradientStop Color="#FFF" Offset="1.0"/> 
    </LinearGradientBrush> 
    <LinearGradientBrush x:Key="PressedBorderBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#444" Offset="0.0"/> 
     <GradientStop Color="#888" Offset="1.0"/> 
    </LinearGradientBrush> 

    <!-- SelectedBackgroundBrush is used for the Selected item in ListBoxItem, ComboBoxItem--> 
    <SolidColorBrush x:Key="SelectedBackgroundBrush" Color="#DDD"/> 

    <!-- Disabled Brushes are used for the Disabled look of each control --> 
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888"/> 
    <SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#EEE"/> 
    <SolidColorBrush x:Key="DisabledBorderBrush" Color="#AAA"/> 

    <!-- Used for background of ScrollViewer, TreeView, ListBox, Expander, TextBox, Tab Control --> 
    <SolidColorBrush x:Key="WindowBackgroundBrush" Color="#FFF"/> 

    <!-- DefaultedBorderBrush is used to show KeyBoardFocus --> 
    <LinearGradientBrush x:Key="DefaultedBorderBrush" EndPoint="0,1" StartPoint="0,0"> 
     <GradientStop Color="#777" Offset="0.0"/> 
     <GradientStop Color="#000" Offset="1.0"/> 
    </LinearGradientBrush> 

    <SolidColorBrush x:Key="SolidBorderBrush" Color="#888"/> 
    <SolidColorBrush x:Key="LightBorderBrush" Color="#AAA"/> 
    <SolidColorBrush x:Key="LightColorBrush" Color="#DDD"/> 

    <!-- Used for Checkmark, Radio button, TreeViewItem, Expander ToggleButton glyphs --> 
    <SolidColorBrush x:Key="GlyphBrush" Color="#444"/> 


    <!-- Style and Template pairs are used to define each control Part --> 
    <!-- The Style provides default values on the control; the Template gives the elements for each control --> 

    <!-- SimpleButtonFocusVisual is used to show keyboard focus around a SimpleButton control --> 
    <Style x:Key="SimpleButtonFocusVisual"> 
     <Setter Property="Control.Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Border> 
         <Rectangle Margin="2" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <!-- Simple Button - This control sets brushes on each state. Note that these brushes must be listed above since they are static resources --> 
    <Style TargetType="{x:Type Local:MyButton}"> 
     <Setter Property="FocusVisualStyle" Value="{DynamicResource SimpleButtonFocusVisual}"/> 
     <Setter Property="Background" Value="{DynamicResource NormalBrush}"/> 
     <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Local:MyButton}"> 

        <!-- We use Grid as a root because it is easy to add more elements to customize the button --> 
        <Grid x:Name="Grid"> 
         <Border x:Name="Border" CornerRadius="3" Background="{TemplateBinding Background}" 
           BorderBrush="{TemplateBinding BorderBrush}" 
           BorderThickness="{TemplateBinding BorderThickness}" 
           Padding="{TemplateBinding Padding}"/> 

         <!-- Content Presenter is where the text content etc is placed by the control --> 
         <!-- The bindings are useful so that the control can be parameterized without editing the template --> 
         <StackPanel Height="Auto" Orientation="Horizontal"> 
          <Image Source="{TemplateBinding ImageSource}" Width="24" Height="24" Stretch="Fill"/> 
          <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Left" Foreground="{DynamicResource TaskButtonTextBrush}" FontWeight="Bold" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" /> 
         </StackPanel> 
        </Grid> 

        <!--Each state sets a brush on the Border in the template --> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsKeyboardFocused" Value="true"> 
          <Setter Property="BorderBrush" Value="{DynamicResource DefaultedBorderBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" Value="{DynamicResource MouseOverBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="true"> 
          <Setter Property="Background" Value="{DynamicResource PressedBrush}" TargetName="Border"/> 
          <Setter Property="BorderBrush" Value="{DynamicResource PressedBorderBrush}" TargetName="Border"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="true"/> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/> 
          <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/> 
          <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <!--<Style TargetType="{x:Type Local:MyButton}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Local:MyButton}"> 
        <StackPanel Height="Auto" Orientation="Horizontal"> 
         <Image Source="{TemplateBinding ImageSource}" Width="24" Height="24" Stretch="Fill"/> 
         <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Left" Foreground="{DynamicResource TaskButtonTextBrush}" FontWeight="Bold" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" /> 
        </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style>--> 



<Grid> 
    <Local:MyButton ImageSource="/WpfApplication1;component/MoveRight.png" Margin="33,101,131,127"> 
     Kishore 
    </Local:MyButton> 

</Grid> 
+1

謝謝。但它不保留原始按鈕的基本風格。 – Archie 2010-04-29 04:16:49