Mark
Mark

Reputation: 14930

WPF Bind scale of one element to the scale of another

I have a button that I want to bind the scaleX and scaleY to the scaleX and scaleY of another element, how can I do that?

The tricky part is that I want to bind it to an element that, at least on initialisation, may not have a ScaleTransform set on it...

Upvotes: 1

Views: 3202

Answers (1)

itowlson
itowlson

Reputation: 74802

Ideally, set it up so that the bindee element does always have a ScaleTransform, but it's the identity transform. Then name that ScaleTransform, and bind to it using the usual element-to-element syntax:

<TextBlock Text="One">
  <TextBlock.LayoutTransform>
    <ScaleTransform ScaleX="1"
                    ScaleY="1"
                    x:Name="s" />  <!-- Note the x:Name -->
  </TextBlock.LayoutTransform>
</TextBlock>

<TextBlock Text="Two">
  <TextBlock.LayoutTransform>
    <ScaleTransform ScaleX="{Binding ScaleX, ElementName=s}"
                    ScaleY="{Binding ScaleY, ElementName=s}" />
  </TextBlock.LayoutTransform>
</TextBlock>

If you need to bind to the element specifically, you can traverse down through the properties e.g. {Binding LayoutTransform.ScaleX, ElementName=someElement}. This will no-op if the source element has no LayoutTransform or it isn't a ScaleTransform, but it will report runtime binding errors.

Upvotes: 5

Related Questions