4

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?

RM.
  • 1,984
  • 19
  • 29
  • 1
    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: https://stackoverflow.com/a/4675314/194717 – Tony Feb 24 '19 at 01:07
  • yes @Tony arguably a duplicate question too, but...that's picking nits. – kenny Feb 25 '19 at 18:42
  • 1
    @Tony I don't think it's duplicate. Background is not defined on FrameworkElement. Margin is – RM. Feb 27 '19 at 08:42
  • 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 Feb 27 '19 at 23:58
  • @tony Ok, sorry I misunderstood. – RM. Feb 28 '19 at 06:42
  • It is a good question... I notice Control and ButtonBase also have the same behaviour. However if you give the style a key name and assign it to the controls directly, it works. Could be something in the default templates but would take some digging. – Jeff R. Feb 28 '19 at 16:27

2 Answers2

2

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>

RM.
  • 1,984
  • 19
  • 29
1
 <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>
Ghotekar Rahul
  • 322
  • 3
  • 10