Question

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>
Was it helpful?

Solution

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top