具有其他目标类型样式的自定义按钮
Custom button with style for another target type
我为按钮创建了自定义控件。我的控件是 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 does not match type of element IconButton'
我是怎么解决这个问题的? IconButton 必须具有 IconButton 的样式,但我只有 Button 的样式。
感谢您的建议。
如果您希望能够将 TargetType 为 Button 的样式应用于自定义 IconButton IconButton class 必须 继承自 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 的控件。
我为按钮创建了自定义控件。我的控件是 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 does not match type of element IconButton'
我是怎么解决这个问题的? IconButton 必须具有 IconButton 的样式,但我只有 Button 的样式。
感谢您的建议。
如果您希望能够将 TargetType 为 Button 的样式应用于自定义 IconButton IconButton class 必须 继承自 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 的控件。