如何在WPF UserControl中创建样式?

时间:2009-09-10 22:19:58

标签: wpf silverlight xaml styles

我想在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>

1 个答案:

答案 0 :(得分:12)

首先,将您的样式放入.Resources标记 - 它可以是几乎任何控件标记的子代(例如,border,usercontrol,grid等) 第二,您可以在标记中指定样式,但由于您没有在资源上声明x:键,因此该样式将应用于此控件中的所有边框。

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

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