A day with .Net

My day to day experince in .net

Archive for the ‘WPF’ Category

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>

Posted in WPF | 1 Comment »