A day with .Net

My day to day experince in .net

Applying a uniform style to all child elements in a Grid – WPF Tips

Posted by vivekcek on March 25, 2013

So you have Grid with some TextBlock, TextBox and Button controls.How can we apply a Margin to all such controls coming inside Grid.

The solution is Given below

 

<Window.Resources>
        <Style  x:Key="Margin">
            <Setter Property="Control.Margin" Value="5"></Setter>
        </Style>
        <Style TargetType="Grid" x:Key="GridStyle">
            <Style.Resources>
                <Style TargetType="TextBlock" BasedOn="{StaticResource Margin}"></Style>
                <Style TargetType="Button" BasedOn="{StaticResource Margin}"></Style>
                <Style TargetType="TextBox" BasedOn="{StaticResource Margin}"></Style>
            </Style.Resources>
        </Style>
    </Window.Resources>

<Grid Background="Beige" Style="{StaticResource GridStyle}" x:Name="grdProduct">

</Grid>

Advertisements

One Response to “Applying a uniform style to all child elements in a Grid – WPF Tips”

  1. Walter Wittel said

    Excellent example. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s