views:

272

answers:

1

This is my XAML:

<TextBlock Name="SeverityText"
           Grid.Column="1"
           Grid.Row="0"
           Foreground="Red">
    <TextBlock.Triggers>

        <DataTrigger Binding="{Binding Path=Severity}">
            <DataTrigger.Value>
                <sm:Severity>Warning</sm:Severity>
            </DataTrigger.Value>
            <Setter TargetName="SeverityText"
                    Property="Foreground"
                    Value="Yellow" />
        </DataTrigger>
                 <DataTrigger Binding="{Binding Path=Severity}">
            <DataTrigger.Value>
                <sm:Severity>Information</sm:Severity>
            </DataTrigger.Value>
            <Setter TargetName="SeverityText"
                    Property="Foreground"
                    Value="White" />
        </DataTrigger>


    </TextBlock.Triggers>
    <TextBlock>Severity:</TextBlock>
    <TextBlock Text="{Binding Path=Severity}" />
</TextBlock>

This is my error message:

Cannot find the static member 'ForegroundProperty' on the type 'ContentPresenter'.

sm:Severity is an enumeration I imported.

+1  A: 

Your triggers and setters need to be defined in a style, rather than on the TextBlock directly:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>

               <DataTrigger Binding="{Binding Severity}"> 
                   <DataTrigger.Value> 
                       <sm:Severity>Warning</sm:Severity> 
                   </DataTrigger.Value> 
                   <Setter TargetName="SeverityText" 
                           Property="Foreground" 
                           Value="Yellow" /> 
               </DataTrigger>

            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
Matt Hamilton
Ok, so what are triggers outside of style blocks for?
Jonathan Allen
@Jonathan I haven't used them much myself, but I believe that the Triggers on a control directly are for event triggers, like catching mouse events etc.
Matt Hamilton