无法使用DataBinding更新文本框属性

时间:2009-06-18 16:53:05

标签: wpf data-binding binding textblock

我有一个自定义窗口,它有两个依赖属性:布尔值? ValidationStatus和字符串ValidationMessage。绑定这些属性可以正常工作,但是当这些值发生更改时,似乎不会触发触发器。我做错了什么?

<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 个答案:

答案 0 :(得分:1)

样式设置器不会覆盖本地属性设置。因此,数据触发器的值被忽略,因为您已在TextBlock上指定了Text和Background属性。要解决此问题,请在样式中设置这些属性的默认值,如以下代码所示:

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