属性 "Style" 只能设置一次

The property "Style" can only be set once

我正在尝试让 this answer 工作,但是我收到错误 The property "Style" can only be set once.。这是为什么?

这是我的:

<UserControl x:Class="SFVControls.Controls.Basic.BooleanRectangleView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="20" d:DesignWidth="40">

    <StackPanel Name="stackTextPanel" Orientation="Horizontal">
        <StackPanel.Style>
            <Setter Property="Margin" Value="0,8,0,0" />
            <Style TargetType="{x:Type StackPanel}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="False">
                        <Setter Property="Margin" Value="0,8,0,0" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="True">
                        <Setter Property="Margin" Value="0,48,0,0" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>
    </StackPanel>
</UserControl>

您有两个 <StackPanel.Style> 的直接子代:属于 StyleMarginSetterStyle 本身。您只能将一个东西分配给 <StackPanel.Style>,并且那个东西必须是 Style

因此只需将 Margin setter 移动到 Style 元素内即可:

<StackPanel Name="stackTextPanel" Orientation="Horizontal">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">


            <!-- Gotta be inside the Style, not outside. -->
            <Setter Property="Margin" Value="0,8,0,0" />


            <Style.Triggers>
                <DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="False">
                    <Setter Property="Margin" Value="0,8,0,0" />
                </DataTrigger>
                <DataTrigger Binding="{Binding QuickDrawBarPinned}" Value="True">
                    <Setter Property="Margin" Value="0,48,0,0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
</StackPanel>

简而言之,你的风格很时尚失礼