Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 627
MedicineMan Avatar asked Sep 10 '09 22:09

MedicineMan


1 Answers

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"

like image 57
Muad'Dib Avatar answered Sep 28 '22 07:09

Muad'Dib