如何在WPF UserControl中创建样式?

Med*_*Man 5 silverlight wpf 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>
Run Code Online (Sandbox Code Playgroud)

Mua*_*Dib 12

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

<UserControl.Resources>
    <Style TargetType="{x:Type Border}">
        <Setter  Property="Width" Value="10"/> 
    </Style>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)

请注意,silverlight的语法不同.而不是TargetType="{x:Type Border}"你会使用TargetType="Border"