Question

One of my Grid currently starts with the following code:

<Grid x:Name="Top_GRID" Margin="4.953,10" Width="817.28">
   <Grid.Resources>
   <Style TargetType="TextBlock">
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Margin" Value="3"/>
      <Setter Property="Background" Value="Red" />
   </Style>
   <Style TargetType="TextBox">
      <Setter Property="VerticalAlignment" Value="Center"/>
      <Setter Property="Margin" Value="3"/>
   </Style>
   <Style TargetType="Button">
       <Setter Property="VerticalAlignment" Value="Center"/>
       <Setter Property="Margin" Value="3"/>
   </Style>
</Grid.Resources>

Just to clarify - I want to declare a Grid in which all TextBlocks are having the Background property set to "Red". All Button margins are set to "3" and so on. I'd like to move the resource definition to a dictionary file.
Should I somehow wrap it as a Style? If so, I'm going to have a recursive style declaration which (I think is illegal).
Sounds simple but I can't find the way to do it.

Was it helpful?

Solution

Try this

<Style x:Key="Grid_ControlStyle" TargetType="Grid">
        <Style.Resources>                
                <Style TargetType="TextBlock">
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="Margin" Value="3"/>
                    <Setter Property="Background" Value="Red" />
                </Style>
                <Style TargetType="TextBox">
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="Margin" Value="3"/>
                </Style>
                <Style TargetType="Button">
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="Margin" Value="3"/>
                </Style>             
        </Style.Resources>
    </Style>

OTHER TIPS

You Need to put all styles for button, TextBox etc. in resourceDictionary file. Then add this file in application resources:

<Application x:Class="Aplication.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        >  
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\YourResource.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then in xaml you will be able to use it like this

 <TextBlock Grid.Column="1" Grid.Row="1" Text="bla bla" Style="{DynamicResource YourStyle}"/>

Finally your style should look like

 <Style x:Key="StyleName" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="3,3,3,3"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12pt"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

Hope thats what you was looking for.

Please go through the concept of resource dictionary in WPF. Any style, colors, fonts etc whatever is related to look of the application which you wish to be repeated in more than one screen of your application shpuld be placed in the resource dictionary.

x:Key is the property which can be used to access the style anywhere across the application. For your resource dictionary to be accessible througout the application, place in it app.xaml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top