MedicineMan
MedicineMan

Reputation: 15322

How do I create a Style within a WPF UserControl?

I want to set the style of some controls on my UserControl, but can't seem to find the right syntax:

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

Upvotes: 5

Views: 8516

Answers (1)

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29266

first, place your styles into a .Resources tag--which can be the child of pretty much any control tag (eg. border, usercontrol, grid, etc.) second, you can specify the style in the tag, but since you didnt declare an x:key on your resource, the style will apply to ALL borders in this control.

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

note that the syntax is different for silverlight. instead of TargetType="{x:Type Border}" you would use TargetType="Border"

Upvotes: 13

Related Questions