具有另一种目标类型

时间:2016-12-12 10:17:24

标签: wpf custom-controls

我为按钮创建了自定义控件。我的控件是IconButton的类型。但我为Button类型定义了样式SuccessButton。我不会将TargetType从Button更改为IconButton,因为BaseOn已设置为来自mahapps的Button。

这是我的代码:

Style.xaml:

<Style x:Key="SuccessButton" TargetType="Button" BasedOn="{StaticResource MetroFlatButton}">
    <Setter Property="Background" Value="#FF449D44"/>
    <Setter Property="Foreground" Value="#fff"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#4AAA4A"/>
        </Trigger>
    </Style.Triggers>
</Style>

IconButton:

 <Button Margin="0 0 0 0" VerticalAlignment="Center" ToolTipService.Placement="Bottom">
        <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>

代码背后:

 public partial class IconButton
    {
        public static readonly DependencyProperty MessageProperty 
            = DependencyProperty.Register("Message", typeof(string), typeof(IconButton));

        public static readonly DependencyProperty IconProperty
            = DependencyProperty.Register("Icon", typeof(Canvas), typeof(IconButton));

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

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

        public IconButton()
        {
            InitializeComponent();
        }
    }

使用方法:

样式有错误:

  

'Button TargetType与元素IconButton'

的类型不匹配

我是如何解决这个问题的? IconButton必须具有IconButton的样式,但我只有Button的样式。

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

如果您希望能够将TargetType为Button的Style应用于自定义IconButton,则IconButton类必须从Button继承:

public partial class IconButton : System.Windows.Controls.Button
{
    public static readonly DependencyProperty MessageProperty
        = DependencyProperty.Register("Message", typeof(string), typeof(IconButton));

    public static readonly DependencyProperty IconProperty
        = DependencyProperty.Register("Icon", typeof(Canvas), typeof(IconButton));

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

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

    ...
}

如果它实际上没有按钮,那么无法将按钮样式应用于实际上不是按钮的控件。