2009-09-10 58 views
5

我想设置我的用户一些控件的整体风格,但似乎无法找到正确的语法:如何在WPF UserControl中创建样式?

<UserControl x:Class="HiideSRM.WIDSModule.BiometricStatusIndicator" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       > 

    <Style TargetType="{x:Type Border}"> 
     <Setter Property="Width" Value="10"/> 
    </Style> 
    <StackPanel Orientation="Horizontal" x:Name="Panel"> 
     <Border Height="50" Margin="1"/> 
     <Border Height="10" Margin="1"/> 
     <Border Height="10" Margin="1"/> 
     <Border Height="10" Margin="1"/> 
    </StackPanel> 

</UserControl> 

回答

12

首先,把你的风格融为.resources标签 - 它可以是孩子几乎任何控件标签(例如边框,用户控件,网格等) 秒,您可以在标签中指定样式,但由于您没有在资源上声明x:键,所以样式将应用于所有边框在这个控制。

<UserControl.Resources> 
    <Style TargetType="{x:Type Border}"> 
     <Setter Property="Width" Value="10"/> 
    </Style> 
</UserControl.Resources> 

请注意,silverlight的语法不同。而不是TargetType="{x:Type Border}"您可以使用TargetType="Border"

相关问题