StyleTrigger Setter a otro elemento

I have the following xaml-Code:

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

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

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

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

Actualizado para comentarios:

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

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.