2009-03-10 25 views
4

Projemin tüm kullanıcı denetimlerinin arkaplan özelliğini ayarlamak istiyorum. WPF UserControl Style

Ben

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

O derler ama işe yaramadı ile çalıştı.

¿Herhangi bir fikir? Teşekkürler! Bunu deneyin

:

+0

Derleme hatası neydi? –

cevap

2

Biraz çift tırnak eksik düşünüyorum

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

yalnızca belirli bir sınıfa aa stili ayarlamak, bu nedenle bu (çalışmak, bir UserControl nesnesi değil yaratacak) çok yararlı:

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

Ama (UserControl türetilmiş bir sınıf) oluşturun gelmez bu:

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

ya açıkça Style özelliğini kullanarak stili ayarlanır Ne yapabilirsiniz:

<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> 
: Her türetilmiş sınıf için bir stil

<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> 

veya oluşturmak, stil içeriği çoğaltma önlemek için BasedOn kullanabilirsiniz

+0

Bunlar WPF'de stil oluşturmak için mevcut iki seçenektir. –

+0

neden x: işaretleme uzantısı? TargetType = "UserControl" benim için çalışıyor .. – markmnl

+0

@ Fëanor - x: Tür bazen isteğe bağlıdır – Nir