“风格”属性只能设置一次

时间:2016-05-17 14:48:56

标签: c# wpf xaml datatrigger

我想让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>

1 个答案:

答案 0 :(得分:5)

您有<StackPanel.Style>的两个直接子女:Setter的{​​{1}}属于MarginStyle本身。您只能为Style分配一个内容,而该内容必须是<StackPanel.Style>

所以只需在Style元素中移动Margin setter:

Style

简而言之,你的风格有一种时尚 faux pas