【发布时间】:2019-07-17 18:20:57
【问题描述】:
我想使用样式为所有控件和 TextBlocks 设置边距。这是我没有使用样式的窗口 XAML:
<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>
这是预期的结果:
我明白 TextBlock 是 FrameWorkElement 而 TextBox & Button 是一个控件(它是一个 FrameWorkElement)。在 FrameWorkElement 上引入了 Margin 属性,因此我尝试在 FrameWorkElement 上设置 Margin 但没有成功:
<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>
如何使用样式设置所有框架元素的边距?
【问题讨论】:
-
有趣的问题。我在这里尝试在 App.xaml 上的 FrameworkElement 上应用样式 Margin,但它似乎不起作用。我能找到的最接近的是:stackoverflow.com/a/4675314/194717
-
是的@Tony 也可以说是一个重复的问题,但是......这是挑剔的。
-
@Tony 我不认为它是重复的。背景未在 FrameworkElement 上定义。保证金是
-
我没有说它是重复的。我说我发现了一个类似的问题,我也无法制作适用于 FrameworkElement 的样式。我对这个问题投了赞成票。
-
@tony 好的,抱歉我误会了。