Maximus
Maximus

Reputation: 3448

DataTrigger from interaction not working Windows Phone

I have simple example and databinding using TemplatedParent is not working. Does anyone have idea what is wrong?

  <Button Background="Red" Content="xD">
        <Button.Template>
            <ControlTemplate TargetType="Button">
                <Border>
                    <i:Interaction.Triggers>
                        <ec:DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Background}" Value="Red">
                            <ec:ChangePropertyAction PropertyName="Background" Value="CadetBlue"/>
                        </ec:DataTrigger>
                    </i:Interaction.Triggers>
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>

No error in output. Background is supposed to be set to CadetBlue but no effect is applied.

Upvotes: 2

Views: 302

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

It seems that RelativeSource.TemplatedParent works fine inside a template, but the triggers don't work as expected: if the trigger condition matches initially, the trigger/action doesn't fire. If you change the bound property programmatically, the trigger will fire. That's why it works with IsPressed: the button didn't start out pressed; it was pressed after it was loaded.

If you move the triggers out of the template and attach them directly to the button and adjust the bindings accordingly, everything should just work:

<Button x:Name="_button"
        Background="Red"
        Content="xD">
  <i:Interaction.Triggers>
    <ei:DataTrigger Binding="{Binding ElementName=_button, Path=Background.Color}"
                    Value="Red">
      <ei:ChangePropertyAction PropertyName="Background"
                               Value="CadetBlue" />
    </ei:DataTrigger>
  </i:Interaction.Triggers>
  <Button.Template>
    <ControlTemplate TargetType="Button">
      <Border Background="{TemplateBinding Background}">
        <ContentPresenter />
      </Border>
    </ControlTemplate>
  </Button.Template>
</Button>

Note the background trigger must bind to Background.Color; it doesn't work if you bind to the brush itself, possibly because SolidColorBrush does not override Equals.

Upvotes: 1

Related Questions