Search code examples
c#wpfxamldatatrigger

The property "Style" can only be set once


I am trying to get this answer to work, however I get the error The property "Style" can only be set once.. Why is that?

This is what I have:

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

Solution

  • You've got two direct children of <StackPanel.Style>: A Setter for Margin that belongs within the Style, and the Style itself. You can only assign one thing to <StackPanel.Style>, and that thing has to be a Style.

    So just move the Margin setter inside the Style element:

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

    In short, your style has a fashion faux pas.