【问题标题】:Custom button with style for another target type具有其他目标类型样式的自定义按钮
【发布时间】:2016-12-12 22:32:09
【问题描述】:

我为按钮创建了自定义控件。我的控件是 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>

图标按钮:

 <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 的样式。

感谢您的建议。

【问题讨论】:

  • 我猜你的TargetType 应该类似于TargetType="{x:Type Button}
  • @AshishSrivastava TargetType="Button" 也可以。

标签: wpf custom-controls


【解决方案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); }
    }

    ...
}

如果不是,则实际上没有 Button,并且您不能将 Button 样式应用于实际上不是 Button 的控件。

【讨论】:

    猜你喜欢
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多