StyleTrigger Setter a otro elemento

I have the following xaml-Code:

<Border x:Name="border">
            <Style TargetType="{x:Type Border}">
                    <DataTrigger Binding="{Binding MessageType}" Value="Error">
                        <Setter Property="Background" Value="Red" />
                <Style TargetType="{x:Type Rectangle}">
                        <DataTrigger Binding="{Binding MessageType}" Value="Error">
                            <Setter Property="Fill" Value="Icon.ico" />

(MessageType is a Enum)

How can i combine this two StyleTrigger into One? Is it possible?

preguntado el 09 de marzo de 12 a las 14:03

1 Respuestas

Not sure what your parent object is, containing the border. But i assume a DataTemplate, other objects should be work similar.

    <Border x:Name="border">
        <Rectangle x:Name="rect"/>

        <DataTrigger Binding="{Binding MessageType}" Value="Error">
            <Setter TargetName="border" Property="Background" Value="Red" />
            <Setter TargetName="rect" Property="Fill" Value="Icon.ico" />

Actualizado para comentarios:

        <DataTemplate x:Key="myTemplate">
            <!-- above template -->
        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource myTemplate}"/>

respondido 12 mar '12, 11:03

The Parent object is a Grid. And how i use the DataTempate? I know it only in combination with lists not to a single object. - San

@San Ok whats the parent object of the Grid? If one parent object up the tree is styleable you can replace DataTemplate with a Style for that particular object and make the Border and Rectangle part of the ControlTemplate. You could also place a ContentControl in the place of the Border and set my DataTemplate as the ContentTemplate. About the list: The DataTemplate in a list, is used on a ListItem, which is also just a single object usually applied through a ContentPresenter. - hacer mientras

Can you give me a example please? Given Tree is: UserControl -> Grid.. in this Grid i can put everything. - San

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