How do I bind to another control’s property from a trigger?

You need to specify the trigger as part of a style — the Triggers collection on the Button itself can only contain event triggers. With that in mind, a DataTrigger works fine. However, there is a wrinkle: the value from the Trigger Setter won’t overwrite a local Content property. So you have to set the default Content in the Style as well. Here’s how it looks:

<Button>  <!-- Note no content set directly on button -->
  <Button.Style>
    <Style TargetType="Button">
      <Setter Property="Content" Value="You may write!!!" />  <!-- Here is the 'normal' content -->
      <Style.Triggers>
        <!-- Here is how we bind to another control's property -->
        <DataTrigger Binding="{Binding IsReadOnly, ElementName=textBox}" Value="True">
          <Setter Property="Content" Value="NO NO NO" />  <!-- Here is the 'override' content -->
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Button.Style>
</Button>

Leave a Comment

techhipbettruvabetnorabahisbahis forumu