tags:

views:

312

answers:

1

I have a custom window which have two depencency properties: Boolean? ValidationStatus, and string ValidationMessage. Binding these properties works fine but trigger doesn't seem to be triggered when these values change. What am I doing wrong?

<TextBlock x:Name="validationTextBox" 
    Grid.Row="1" 
    Grid.ColumnSpan="2" 
    Text="{Binding ElementName=_this, Path=ValidationMessage}"
    TextAlignment="Center"
    Background="Green">

    <TextBlock.Style>
      <Style>
        <Style.Triggers>
          <DataTrigger Value="False" Binding="{Binding ElementName=_this, Path=ValidationStatus}">
            <Setter Property="Panel.Background" Value="Red"/>
            <Setter Property="TextBox.Text" Value="Outer checkbox is not checked"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </TextBlock.Style>

</TextBlock>
+1  A: 

Style Setters do not override local attribute settings. Therefore the data trigger's values are being ignored because you have specified the Text and Background properties on the TextBlock. To fix the problem set the default values of these properties in the style as shown in the following code:

<TextBlock x:Name="validationTextBox" 
           Grid.Row="1" 
           Grid.ColumnSpan="2" 
           TextAlignment="Center">

<TextBlock.Style>
  <Style>
    <Setter Property="TextBox.Text" Value="{Binding ElementName=_this, Path=ValidationMessage}"/>
    <Setter Property="TextBox.Background" Value="Green"/>
    <Style.Triggers>
      <DataTrigger Value="False" Binding="{Binding ElementName=_this, Path=ValidationStatus}">
        <Setter Property="TextBox.Background" Value="Red"/>
        <Setter Property="TextBox.Text" Value="Outer checkbox is not checked"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</TextBlock.Style>

Tony Borres
Thanks, you are right to the point, though I found one things in you example not working, background wasn't updated. To make it work I had to change "Panel.Background" to "TextBlock.Background".
Sergej Andrejev