Pregunta

Tengo una ventana personalizada que tiene dos propiedades de dependencia: booleana? ValidationStatus y string ValidationMessage. El enlace de estas propiedades funciona bien, pero el disparador no parece activarse cuando cambian estos valores. ¿Qué estoy haciendo mal?

<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>
¿Fue útil?

Solución

Los

Style Setters no anulan la configuración de los atributos locales. Por lo tanto, los valores del activador de datos se ignoran porque ha especificado las propiedades de Texto y Fondo en TextBlock. Para solucionar el problema, configure los valores predeterminados de estas propiedades en el estilo como se muestra en el siguiente código:

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top