WPF自定义控件按钮中的两种样式

问题描述:

我创建了自定义按钮。我想在控件中定义样式,但是它不使用这种样式,因为我在使用此控件时使用了样式。 这是我的代码: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

这里是我的建议:

的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> 

我不能这样做,因为,我没有后面的代码。 就是这样,让我知道如果你需要更多的解释。

最好的问候。