4
votes

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?

2
Interesting question. I tried here to apply a style Margin on FrameworkElement on App.xaml and it does not seems to work. The closest I could find is this: stackoverflow.com/a/4675314/194717Tony
yes @Tony arguably a duplicate question too, but...that's picking nits.kenny
@Tony I don't think it's duplicate. Background is not defined on FrameworkElement. Margin isRM.
I did not say it is duplicate. I said that I found a similar question, and I also could not make a Style that works on FrameworkElement. I Voted Up on this question.Tony
@tony Ok, sorry I misunderstood.RM.

2 Answers

1
votes
 <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>
0
votes

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>