1

テキスト コンテンツを白から黒に変更するマウスオーバー スタイルを指定する標準を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>

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

4

1 に答える 1

1

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

<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 のコンテンツ内のコントロールが明示的に独自のフォアグラウンドを設定しない限り、これは機能します。彼らが独自の色を設定した場合、すべての賭けは無効になります。

于 2009-10-28T23:24:06.193 に答える