Prioridad de estilo sobre encuadernación

In my example, no matter how I define styles for a behavior in my control, it gets only the style defined in the binding property. Ex.:

    <Style TargetType="Border">
        <Setter Property="BorderBrush" Value="Gray"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Style.Triggers>
            <Trigger Property="Border.IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow"/>                    
            </Trigger>
        </Style.Triggers>
    </Style>

    ....

    <Border Background="{Binding UserColor}">

When mouse is over control, the background does not turns yellow.

preguntado el 28 de mayo de 14 a las 12:05

2 Respuestas

That's correct behavior. Remove the direct assignment and add a setter to your style:

<Style TargetType="Border">
    <Setter Property="BorderBrush" Value="Gray"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Background" Value="{Binding UserColor}"/>
    <Style.Triggers>
        <Trigger Property="Border.IsMouseOver" Value="True">
            <Setter Property="Background" Value="Yellow"/>                    
        </Trigger>
    </Style.Triggers>
</Style> 

contestado el 28 de mayo de 14 a las 12:05

LA Control.Background la propiedad es una DependencyProperty y DependencyPropertys can be set from a variety of sources, eg. Style Setter, Animation, from code, etc. Because of this reason, Microsoft had to define an order of precedence to decide which of these possible ways to change the DependencyProperty value were more important than others.

It was logically decided that setting a DependencyProperty using inline XAML as you did in your example should 'override' values set from other sources, eg. from a Style. Es por eso que el Background a partir de su Style is not showing in your example. For full information of the DependencyProperty value precedence list, please see the Precedencia de valor de propiedad de dependencia página en MSDN.


ACTUALIZAR >>>

Note that if you wanted to set a default Background colour for your Border, then you should declare it in the used Style y no inline, as we have already learned that that doesn't work:

<Style TargetType="Border">
    <Setter Property="BorderBrush" Value="Gray"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Background" Value="{Binding UserColor}"/> <!-- Default -->
    <Style.Triggers>
        <Trigger Property="Border.IsMouseOver" Value="True">
            <Setter Property="Background" Value="Yellow"/>                    
        </Trigger>
    </Style.Triggers>
</Style>

contestado el 28 de mayo de 14 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.