質問

テキストコンテンツを白から黒に変更するマウスオーバースタイルを指定する標準の Button Style をアプリケーション全体に適用しています。これを実現するために、ボタンテンプレートを変更し、 ContentPresenter を削除して、 TextBlock に置き換えてみました。ただし、これは次のような混合コンテンツを使用できないことを意味します。

<Button
    <Button.Content>
        <StackPanel Orientation="Horizontal">
            <Path Margin="3" Width="12.375" Height="9.70833" Canvas.Left="0" Canvas.Top="0"
                  Stretch="Fill" Fill="#FF115485"
                  Data="F1 M 18.8333,7.08333L 16.7083,4.91667L 10.5,10.5417L 8.52083,8.6875L 6.45833,10.4792L 10.4792,14.625L 18.8333,7.08333 Z "/>
            <TextBlock Margin="3">Hello</TextBlock>
        </StackPanel>
    </Button.Content>
</Button>

状況を改善するために、 ContentPresenter を戻し、(テキストのみの)ボタンで次のように ContentTemplate を指定しました:

<Button IsDefault="True" Content="Text only Button"
        ContentTemplate="{StaticResource TextButtonTemplate}"
        Command="{Binding ...}" 
        CommandParameter="{...}" />

そして、以下のように定義された Template を使用します。このテンプレートは、テキストコンテンツを含む Button でのみ機能し、混合コンテンツの必要に応じて他のテンプレートが定義されます。

<DataTemplate x:Key="TextButtonTemplate">
    <TextBlock Text="{TemplateBinding Content}"
               Foreground="{Binding Path=Foreground,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Button}}" />
</DataTemplate>

カスタムテンプレートを定義せずに、目的のマウスオーバースタイルを維持することでこれをさらに改善できる方法があるかどうか、誰もが知っていますか?

役に立ちましたか?

解決

コンテンツがそれ自体で明示的に色を設定しないと仮定すると、次のようにこれを達成できます:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="TextBlock.Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>

これは、TextBlock.Foregroundが継承されたプロパティであるため機能します。 Buttonのコンテンツ内のコントロールが明示的に前景を設定しない限り、これは機能します。独自の色を設定すると、すべてのベットがオフになります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top