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で変更できるようにします。