2010-11-26 13 views
7

さて、私は私のファイルStyles.xamlのthatsのは、それがすべてのものに適用されますので、エディタでイムこれが動作しているようです場合は、ここでWPFスタイルのボタンのfontSizeが変更されますか?

が私のスタイル

<Style TargetType="{x:Type Control}" x:Key="baseStyle"> 
    <Setter Property="FontFamily" Value="Verdana"/> 
    <Setter Property="FontSize" Value="12"/> 
</Style> 

<Style TargetType="Button" BasedOn="{StaticResource baseStyle}"> 
    <Setter Property="Margin" Value="2,0,2,0"/> 
    <Setter Property="Padding" Value="2"/> 
    <Setter Property="FontSize" Value="50"/> 
</Style> 

<Style TargetType="TextBlock"> 
    <Setter Property="FontFamily" Value="Verdana"/> 
    <Setter Property="FontSize" Value="12"/> 
</Style> 

..ですApplication.xamlに合併したが持っていますアプリケーションを実行すると、ボタンのフォントサイズが通常のサイズに縮小されます。

コンテンツは文字列に設定され、テキストブロックスタイルを使用するとボタンがTextBlockを作成します。どのように私はこれをオーバーライドできますか?

答えて

0

私はあなたのスタイルを試しましたが、うまくいきます。だからあなたのスタイルは問題ではありません。あなたが書いたように、あなたがそのスタイルをマージした場所だと思います。 Application.xamlではなくMainWindowファイルにResourceDictionary Styles.xamlを置く方がよいでしょう。

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

    <Window.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="Styles.xaml" /> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Window.Resources> 

    <StackPanel> 
     <TextBlock>TTT</TextBlock> 
     <Button>BBB</Button> 
    </StackPanel> 
</Window> 

しかし、あなたの問題は、それが解決策ではない場合は、もう少しあなたのコードのこの部分を掲載することにより、あなたのスタイルを使用する方法を明確にすることができ、依然として不明ですか?

8

あなたは右の程度

私の推測では、その内容が 文字列に設定してからテキストブロック スタイル

を使用したときのボタンは のTextBlockを作成するということですね。ポストthisを参照してください。

この問題を回避するには、我々が明示的にコンテンツを表示するには、デフォルト のTextBlockを使用することができます 可能System.String、ため するDataTemplateを定義することです。あなた この のDataTemplateが あなたのスタイルによってもたらさ何のContentPresenter に適用されるようにするDataTemplateが 同じ辞書であなたが のTextBlockのスタイルを定義することを置くことができます。

だからこれは、TextBlockのためのあなたのスタイルを維持しますが、例えばボタンで作成されたのTextBlockが影響されることはありません

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    <Style TargetType="{x:Type Control}" x:Key="baseStyle"> 
     <Setter Property="FontFamily" Value="Verdana"/> 
     <Setter Property="FontSize" Value="12"/> 
    </Style> 

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource baseStyle}"> 
     <Setter Property="Margin" Value="2,0,2,0"/> 
     <Setter Property="Padding" Value="2"/> 
     <Setter Property="Foreground" Value="Red" /> 
     <Setter Property="FontSize" Value="50"/> 
    </Style> 

    <Style TargetType="{x:Type TextBlock}"> 
     <Setter Property="FontFamily" Value="Verdana"/> 
     <Setter Property="Foreground" Value="Green" /> 
     <Setter Property="FontSize" Value="24"/> 
    </Style> 

    <DataTemplate DataType="{x:Type sys:String}"> 
     <TextBlock Text="{Binding}"> 
      <TextBlock.Resources> 
       <Style TargetType="{x:Type TextBlock}"/> 
      </TextBlock.Resources> 
     </TextBlock> 
    </DataTemplate> 
</ResourceDictionary> 

問題を解決しますStyles.xamlに終わりのDataTemplateを追加しますそれによって

+1

私はこれ以上投票することができたらいいですね。これを理解しようとする別の開発者と数時間を過ごしました。ありがとう! – Morinar

+0

この回避策には「不具合」がありますか?たとえば、スタイルをButttonsのTextBlocksにのみ適用し、他のTextBlocksには適用しない場合は、 – matrixugly

関連する問題