【问题标题】:Set margin for all controls and textblocks using WPF style使用 WPF 样式为所有控件和文本块设置边距
【发布时间】: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 好的,抱歉我误会了。

标签: wpf xaml wpf-style


【解决方案1】:

TargetType 必须与 FrameWorkElement 的确切类型匹配。为 FrameWorkElement 定义样式不会将该样式应用于子类(例如 TextBlock)。

因此无法以这种方式设置边距。可以通过为样式添加 Key 并为每个元素一个一个选择此样式来设置边距

<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>

【讨论】:

  • 我使用了 TargetType = control,其中包含大量项目。所以我认为它不必匹配确切的类型。
【解决方案2】:
 <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>

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2016-07-05
    • 2019-05-19
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多