2016-12-12 100 views
0

我創建了自定義按鈕。我想在控件中定義樣式,但是它不使用這種樣式,因爲我在使用此控件時使用了樣式。 這是我的代碼:WPF自定義控件按鈕中的兩種樣式

<Button x:Class="Spd.Client.Controls.IconButton" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:Spd.Client.Controls" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Button.Style> 
     <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}"> 
      DONT WORKED 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="ToolTip" Value="{Binding TooltipMessage}" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
    <StackPanel Orientation="Horizontal"> 
     <Rectangle Width="12" Height="12" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"> 
      <Rectangle.OpacityMask> 
       <VisualBrush Stretch="Fill" Visual="{Binding Path=Icon,RelativeSource={RelativeSource AncestorType={x:Type local:IconButton}}}" /> 
      </Rectangle.OpacityMask> 
     </Rectangle> 
     <TextBlock Text="{Binding Path=Message,RelativeSource={RelativeSource AncestorType={x:Type local:IconButton}}}" FontSize="10" Margin="2 0 0 0"/> 
    </StackPanel> 
</Button> 

用法:

<controls:IconButton Message="Click me!" 
    Icon="{StaticResource appbar_user}" Style="{StaticResource SuccessButton}"  
    TooltipMessage="This is tooltip for disabled button" IsEnabled="False"/> 

用它只是形式= {StaticResource的SuccessButton}和不使用它的按鈕樣式設置提示。 什麼是正確的解決方案?由於

+0

當您設置樣式DP時,您將覆蓋您在XAML中定義的樣式。爲什麼不用XAML basedOn SuccessButton樣式而不是{x:Type Button}聲明樣式? – nkoniishvt

+0

我不知道風格。 IconButton可能有任何樣式(SuccessButton,ErrorButton等)。所以我需要使用按鈕定義的樣式BaseOn bluray

+0

您是否嘗試定義您的SuccessButton樣式BasedOn {x:Type IconButton}? – nkoniishvt

回答

1

這裏是我的建議:

的XAML:

<UserControl x:Class="CustomButtonSOSHelpAttempt.IconButton" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:customButtonSosHelpAttempt="clr-namespace:CustomButtonSOSHelpAttempt"> 
<Button> 
    <Button.Style> 
     <Style BasedOn="{StaticResource {x:Type Button}}" 
       TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="ToolTip" Value="{Binding Path=ToolTipMessage, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type customButtonSosHelpAttempt:IconButton}}}" /> 
        <Setter Property="ToolTipService.ShowOnDisabled" Value="True" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
    <StackPanel Orientation="Horizontal"> 
     <Rectangle Width="12" 
        Height="12" 
        Fill="{Binding Path=Foreground, 
            RelativeSource={RelativeSource FindAncestor, 
                   AncestorType={x:Type customButtonSosHelpAttempt:IconButton}}}"> 
      <Rectangle.OpacityMask> 
       <VisualBrush Stretch="Fill" 
          Visual="{Binding Path=Icon, 
               RelativeSource={RelativeSource AncestorType={x:Type customButtonSosHelpAttempt:IconButton}}}" /> 
      </Rectangle.OpacityMask> 
     </Rectangle> 
     <TextBlock Margin="2 0 0 0" 
        FontSize="10" 
        Text="{Binding Path=Message, 
            RelativeSource={RelativeSource AncestorType={x:Type customButtonSosHelpAttempt:IconButton}}}" /> 
    </StackPanel> 
</Button></UserControl> 

XAML代碼背後:在Window類

public partial class IconButton 
{ 
    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(IconButton), new PropertyMetadata(default(string))); 
    public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(object), typeof(IconButton), new PropertyMetadata(default(object))); 
    public static readonly DependencyProperty ToolTipMessageProperty = DependencyProperty.Register("ToolTipMessage", typeof(object), typeof(IconButton), new PropertyMetadata(default(object))); 

    public IconButton() 
    { 
     InitializeComponent(); 
    } 

    public string Message 
    { 
     get { return (string) GetValue(MessageProperty); } 
     set { SetValue(MessageProperty, value); } 
    } 

    public object Icon 
    { 
     get { return (object) GetValue(IconProperty); } 
     set { SetValue(IconProperty, value); } 
    } 

    public object ToolTipMessage 
    { 
     get { return (object) GetValue(ToolTipMessageProperty); } 
     set { SetValue(ToolTipMessageProperty, value); } 
    } 
} 

用法:

<Window x:Class="CustomButtonSOSHelpAttempt.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:CustomButtonSOSHelpAttempt" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:system="clr-namespace:System;assembly=mscorlib" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    mc:Ignorable="d"> 
<Window.Resources> 
    <Image x:Key="AppbarUser" 
      Source="Res/appbar_user.png" /> 
</Window.Resources> 
<Grid> 
    <local:IconButton Width="100" 
         Height="100" 
         Foreground="#FF00FF00" 
         Icon="{StaticResource AppbarUser}" 
         IsEnabled="False" 
         Message="click me!!!" 
         ToolTipMessage="Tool tip message!!!" /> 
</Grid></Window> 

該解決方案曾先後對我來說, 首先試圖改變在未來的方式風格的解決方案:

<Button.Style> 
     <Style BasedOn="{StaticResource {x:Type Button}}" 
       TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="ToolTip" Value="{Binding Path=ToolTipMessage, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type customButtonSosHelpAttempt:IconButton}}}" /> 
        <Setter Property="ToolTipService.ShowOnDisabled" Value="True" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
</Button.Style> 

我不能這樣做,因爲,我沒有後面的代碼。 就是這樣,讓我知道如果你需要更多的解釋。

最好的問候。