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

69,076

Solution 1

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>

Solution 2

Have you tried this:

<StackPanel x:Name="LayoutRoot">
    <Button Width="75" Content="{Binding IsReadOnly, ElementName=textBox, Mode=Default}" />
    <TextBox x:Name="textBox" VerticalAlignment="Top" Text="TextBox" />
</StackPanel>

??

Share:
69,076
rrhartjr
Author by

rrhartjr

Updated on April 22, 2020

Comments

  • rrhartjr
    rrhartjr about 4 years

    In my particular case, I want to bind to the IsReadOnly property of a TextBox to set the Content property of a Button? They are both part of the same StackPanel.

    I've tried doing it with a DataTrigger with a Binding to the ElementName of the TextBox and a Trigger using the TextBox name as the SourceName.

    Any thoughts?

  • user6170001
    user6170001 about 14 years
    He may want his button to say something more meaningful than True or False though *grin*. Which you can do by inserting a converter, of course, but a trigger does feel more idiomatic...
  • user6170001
    user6170001 about 14 years
    Mark, I edited your code to get rid of some extraneous bits that I felt made it hard to see the core of your suggestion. Hope this is okay -- if you feel I've distorted your intent then please roll back.
  • Mark
    Mark about 14 years
    that's fine, thanks for that, I just whipped it up real quick and probably should have cleaned it up :)
  • rrhartjr
    rrhartjr about 14 years
    Ah ha! I knew there was a property precedence, but it didn't occur to me that direct would overwrite the Trigger action. I'm using a DP as the binding source and ended up with the same lack of behavior as trying to use ElementName, so the problem was actually with the property precdence. Thanks for clearing that up!
  • user1841243
    user1841243 over 10 years
    this is very nice. just what I needed, I was almost going to code a converter. but this is nicer.
  • SteveP
    SteveP over 6 years
    > However, there is a wrinkle: the value from the Trigger Setter won't overwrite a local Content property. This is important ! I missed it the first time I read this answer.
  • IronHide
    IronHide almost 3 years
    Is there a MSFT documentation about those things ? e.g how to make triggers affect other elements in up or down direction, or about that overriding ?