2017-12-19 11 views
0

「x:Key」プロパティを指定しないと、デフォルトスタイルを設定できることがわかります。 (対象タイプ "TargetType"のすべての要素に適用されます) 継承(BasedOn)のキーを使用したいが、それでもデフォルトを維持しています。この:デフォルトスタイルを設定して名前を付けます

<Style TargetType="Button" x:Key="name"> 
     //... 
    </Style > 
    <Style TargetType="Button" BasedOn="name"/> 

しかし、それを行うには、より正確な方法がある場合、私はそれを好む

答えて

1

あなたは{X:タイプ}でその名前を設定することができます。マークアップ拡張機能:

<Style TargetType="{x:Type Button}" x:Key="{x:Type Button}"> 
      <Setter Property="Background" Value="Red"/> 
    </Style> 

これは暗黙のスタイルですが、今から継承することができます。ここで

は、完全な作業のサンプルです:

<Window.Resources> 
     <Style TargetType="{x:Type Button}" x:Key="{x:Type Button}"> 
      <Setter Property="Background" Value="Red"/> 
     </Style> 

     <Style x:Key="AnotherStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}"> 
      <Setter Property="FontSize" Value="22"/> 
     </Style> 
    </Window.Resources> 

    <Grid Background="DimGray" Name="Grid" DataContext="{Binding ExpandoObject}"> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <Button Grid.Row="1" Content="LOL" Style="{StaticResource AnotherStyle}"/> 
    </Grid> 

EDIT 以下のコメントとして、あなたはないイベントがXに設定する必要があります:あなたはXを使用してアクセスすることができますように、キーは:のみ

+0

あなたは正しいです。しかし、x:Key = "{x:Type Button}"を追加する必要はありません。これは、WPFがバックグラウンドであなたのために行います。したがって、暗黙的なスタイル(x:Keyなし)を作成しても、{StaticResource {x:Type Button}} – Pavel

+0

を使用してそれを参照することはできます。私は自分の答えを編集しました。 – taquion

-1

を入力あなたはStaticResourceを使用します:

<Style TargetType="Button" x:Key="name"> 
    //... 
</Style > 
<Style TargetType="Button" BasedOn="{StaticResource name}"> 
    //... 
</Style > 
+0

これは間違っています.x:Keyにx以外の値を指定すると、暗黙的なスタイル動作が失われます。 – taquion

関連する問題