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

时间:2016-12-12 11:38:01

标签: 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"/>

仅使用Style = {StaticResource SuccessButton}并且不要在按钮中使用它来设置工具提示。 什么是正确解决方案?感谢

1 个答案:

答案 0 :(得分: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的代码背后:

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类中的用法:

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

我无法做到这一点,因为我没有让你的代码落后。 这就是全部,如果您需要更多解释,请告诉我。

最诚挚的问候。

相关问题