ボタンのカスタムコントロールを作成しました。私のコントロールはIconButtonの型です。しかし、私はButton型のスタイルSuccessButtonを定義しました。 BaseOnがボタンに設定されているので、TargetTypeをButtonからIconButtonに変更しないでください。mahapps。ここで別のターゲットタイプのスタイルのカスタムボタン
は私のコードです:
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 does not match type of element IconButton'
? IconButtonはIconButtonのスタイルを持っている必要がありますが、私はButtonのスタイルしか持っていません。
アドバイスありがとうございます。
あなたの 'TargetType'は' TargetType = "{x:Type Button}'のようなものでしょうか? –
@AshishSrivastava 'TargetType =" Button "'も同様です。 – Clemens