如何在WPF UserControl中创建样式?

5

我想设置用户控件中某些控件的样式,但似乎找不到正确的语法:

<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个回答

13

首先,将你的样式放入一个.Resources标签中--它可以是任何控件标签的子标签(例如border、usercontrol、grid等)。 其次,你可以在标签中指定样式,但由于你没有在资源上声明x:key,这个样式会应用于此控件中的所有边框。

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

请注意,对于 Silverlight,语法是不同的。你需要使用 TargetType="Border" 而不是 TargetType="{x:Type Border}"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接