RM.
RM.

Reputation: 2043

Set margin for all controls and textblocks using WPF style

I would like to set the margin for all controls and TextBlocks using style. Here is my window XAML without using styles:

<Window x:Class="Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window2" Height="150" Width="300">
    <StackPanel>
        <TextBlock Margin="5" Text="Test" Foreground="White"/>
        <TextBox Margin="5">Test</TextBox>
        <Button Margin="5">Test</Button>
    </StackPanel>
</Window>

and this is the expected result:

enter image description here

I do understand that TextBlock is FrameWorkElement and TextBox & Button is a Control (which is a FrameWorkElement). Margin property is introduced on the FrameWorkElement so I have tried setting Margin on the FrameWorkElement without success:

<Window x:Class="Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Window2" Height="150" Width="300">
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="FrameworkElement">
                <Setter Property="Margin" Value="5"/>
            </Style>
        </StackPanel.Resources>
        <TextBlock Text="Test" Foreground="White"/>
        <TextBox>Test</TextBox>
        <Button>Test</Button>
    </StackPanel>
</Window>

enter image description here

How can I set the margin for all framework element using style?

Upvotes: 4

Views: 2141

Answers (2)

Ghotekar Rahul
Ghotekar Rahul

Reputation: 342

 <Window.Resources>
        <!-- One style for each *type* of control on the window -->
        <Style TargetType="TextBox">
            <Setter Property="Margin" Value="10"/>
        </Style>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10"/>
        </Style>
    </Window.Resources>
    <StackPanel>
        <TextBox Text="TextBox"/>
        <TextBlock Text="TextBlock"/>
    </StackPanel>

Upvotes: 1

RM.
RM.

Reputation: 2043

TargetType must match the exact type of the FrameWorkElement. Defining a style for FrameWorkElement does not apply the style to child classes (e.g. the TextBlock).

So it is not possible to to set the margin this way. It is possible to set the margin by adding a Key to the style and selecting this style for each element one by one

<Window x:Class="Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Window2" Height="150" Width="300">
<StackPanel>
    <StackPanel.Resources>
        <Style x:Key="MX">
            <Setter Property="FrameworkElement.Margin" Value="5"/>
        </Style>
    </StackPanel.Resources>
    <TextBlock Style="{StaticResource MX}" Text="Test"/>
    <TextBox Style="{StaticResource MX}">Test</TextBox>
    <Button Style="{StaticResource MX}">Test</Button>
</StackPanel>

Upvotes: 2

Related Questions