ボタンの背景色を、通常のマウスオーバーおよびプレス状態でさまざまな色合いに変更したいとします。私は、私のボタンにButton.Template
という次の奇妙な冗長を追加して、とRenderPressed
の属性をTemplateBinding
の代わりにBinding
の代わりに使うようにしました。その結果、私のトリガー(Button.Style
)は実際にはコンパイルされず時間性質はTemplateBinding
です。これらの2つの属性をオーバーライドする方法はありますか? XAMLは次のとおりです。コントロールテンプレート全体を繰り返さずに、ボタンのコントロールテンプレート内のTemplateBindingsをBindingsに変更するには
<Window x:Class="ButtonMouseOver.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button>
Hello
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true"
x:Name="Chrome" Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}"
RenderMouseOver="{Binding IsMouseOver}" RenderPressed="{Binding IsPressed}"
>
<ContentPresenter Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Microsoft_Windows_Themes:ButtonChrome>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="LightGreen"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Background" Value="Green"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property = "Foreground" Value="DarkGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
重要な注意を:コントロールテンプレートのみAeroテーマのために必要です。他のテーマについては、Button.Styleだけで仕事をしています。あなたが背景を切り替えるには、スタイルを使用する場合は、ここで行ったよう
私はあなたがこれに対処するためのスタイルを作成せず、あなたが合っているように各ボタンにそれを適用する理由を理解していませんか?私はこの行動に抵抗している作品を見逃しています...?私には標準のボタンスタイルのようです... –
この動作に抵抗している部分は、Aeroクロムです。私はこの事実を強調するためにメモを追加しました。 –