How to give style to wpf controls

2 comments
Here you can see we are giving style to wpf controls in design page only . You will get complete idea about static style in wpf applications. Let's see,

Main window
<Window x:Class="StyleDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="textblock_style" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Right"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
         
        </Style>
        <Style x:Key="textbox_style" TargetType="TextBox">
            <Setter Property="HorizontalAlignment" Value="Left"></Setter>
            <Setter Property="Margin" Value="5"></Setter>
            <Setter Property="Width" Value="150"></Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Name" Grid.Row="0" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="0" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
        <TextBlock Text="Address" Grid.Row="1" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="1" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
        <TextBlock Text="description" Grid.Row="2" Grid.Column="0" Style="{StaticResource textblock_style}"></TextBlock>
        <TextBox  Grid.Row="2" Grid.Column="1" Style="{StaticResource textbox_style}"></TextBox>
    </Grid>
</Window>
Let's see output


2 comments:

  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!


    Web Development Company in Chennai


    ReplyDelete