2009-03-10 21 views
4

私のプロジェクトのすべてのusercontrolのbackgroundプロパティを設定したいと思います。WPF UserControl Style

私はそれをコンパイルしたがうまくいきませんでした

<style TargetType={x:Type UserControl}> 
    <setter property="Background" Value="Red" /> 
</style> 

てみました。

¿任意のアイデア? ありがとう!

+0

コンパイルエラーは何ですか? –

答えて

2

私はあなたには、いくつかの二重引用符が欠落していると思う:

この試してみてください:あなたが唯一の特定のクラスにAAスタイルを設定することができますので、これは動作(ユーザーコントロールオブジェクトを作成し、しません

<Window.Resources> 
    <Style TargetType="{x:Type UserControl}"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 
</Window.Resources> 
<Grid> 
    <UserControl Name="control" Content="content"></UserControl> 
</Grid> 
21

を)非常に便利:

<Window.Resources> 
    <Style TargetType="{x:Type UserControl}"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 
</Window.Resources> 
<Grid> 
    <UserControl Name="control" Content="content"></UserControl> 
</Grid> 

しかし、これは()UserControlから派生したクラスを作成していない:

あなたは何ができるか

が明示的にスタイルプロパティを使用してスタイルを設定されている次のいずれか

<Window.Resources> 
    <Style TargetType="{x:Type UserControl}" x:Key="UCStyle"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 
</Window.Resources> 
<Grid> 
    <l:MyUserControl Name="control" Content="content" Style="{StaticResource UCStyle}"></l:MyUserControl> 
</Grid> 

または各派生クラスのスタイルを作成するには、スタイルの内容の重複を避けるためにBASEDONを使用することができます。

<Window.Resources> 
    <Style TargetType="{x:Type UserControl}" x:Key="UCStyle"> 
     <Setter Property="Background" Value="Red" /> 
    </Style> 
    <Style TargetType="{x:Type l:MyUserControl}" BasedOn="{StaticResource UCStyle}" /> 
</Window.Resources> 
<Grid> 
    <l:MyUserControl Name="control" Content="content"></l:MyUserControl> 
</Grid> 
+0

これは、WPFのスタイリングのために存在する2つのオプションです。 –

+0

x:マークアップ拡張子はなぜですか? TargetType = "UserControl"は私のために働きます.. – markmnl

+0

@Fëanor - x:タイプは時にはオプションです – Nir

関連する問題