1

Silverlightプロジェクトにボタンのカスタムスタイルがあり、ボタンのテキストの前景(およびその他のプロパティ)を設定したいと思います。しかし、私の考えはContentPresenter、スタイルでを使用することです。このようにして、ボタンに好きなものを入れることができます。

ただし、コンテンツとしてテキストがある場合は、Foreground、FontFamily、FontSizeなどの特定のプロパティを設定できるようにしたいです。また、ホバーなどでこれらのプロパティを変更したいと思います。

これは私のスタイルです(簡略化):

<Style x:Key="ButtonStyle" TargetType="Button">
    <!-- There are other properties here, but I left them out for brevity -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="ButtonBorder" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Padding="{TemplateBinding Padding}">
                    <ContentPresenter x:Name="ButtonContent" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                    </ContentPresenter>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <!-- I would like to change the Foreground here -->
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

私が見つけたすべての情報はTextBlock.Foreground="..."、ContentPresenterに追加するように教えてください。

                    <ContentPresenter x:Name="ButtonContent" 
                                      Content="{TemplateBinding Content}" 
                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      TextBlock.Foreground="{TemplateBinding Foreground}">

                    </ContentPresenter>

しかし、これは私にはうまくいきません。アタッチ可能なプロパティForegroundがTextBlockタイプで使用できないというエラーが表示されます。これは、そのソリューションがWPFでのみ機能するためであり、Silverlightでこのプロパティを設定するにはどうすればよいですか?

タグを使用してフォアグラウンドを設定できることはわかってい<Setter>ますが、マウスオーバーでフォアグラウンドを変更するにはどうすればよいですか?ContentPresenterの前景を設定して、MouseOverVisualStateで変更できるようにします。

4

2 に答える 2

1

(私の悪い英語でごめんなさい)

ボタンのプロパティを設定するだけで、機能するはずです。

<Button Style="{StaticResource ButtonStyle}" Content="Test" Foreground="Red" FontFamily="Georgia" FontSize="25"/>

または、テキストボタンのスタイルを作成できます。

<Style x:Key="ButtonTextStyle" TargetType="Button" BasedOn="{StaticResource ButtonStyle}">
    <Setter Property="Foreground" Value="Red"/>
</Style>

そしてそれを使用します:

<Button Style="{StaticResource ButtonTextStyle}" Content="Test"/>

編集:

ContentPresenterの代わりにContentControlを配置してみてください。これにより、StoryBoard内にForegroundPropertyとVisualStateが作成されます。

<ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ButtonContent">
    <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
于 2011-12-03T15:40:35.117 に答える
0

Button @http ://msdn.microsoft.com/en-us/library/cc278069 (v=vs.95).aspxに使用されているベースラインコントロールテンプレートをご覧ください。そのコントロールを取得してから調整することをお勧めします。要件に基づいてマウスオーバーします。だからあなたが赤にマウスオーバーするために、私は次のようなことをします

 <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="BackgroundAnimation" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="Red"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="Red"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[3].(GradientStop.Color)" To="Red"/>
                                    </Storyboard>
                                </VisualState>
于 2011-12-03T16:31:43.977 に答える