Omair
Omair

Reputation: 874

Get a child element property in XAML from root in a generic manner

In the following XAML code:

<CheckBox
          Name="ChkBox_2"
          AutomationProperties.Name = "Bind_TO_AccessText.Text">
          <AccessText
                Name="ChkBox_2AccessText"
                TextWrapping="Wrap">
                _Option 1
          </AccessText>
</CheckBox>

Is there a way to bind the checkbox property to the inner element's property?

This is a commonly used pattern where we want to format the text of the checkbox, and also set the accessible name i.e. AutomationProperties.Name.

I would want to use a binding so that I could set the AutomationProperties.Name in a style for all such checkboxes. I cannot bind using ElementName, since that wouldn't be generic.

Aside from this binding, what is the recommended way to do this?

Upvotes: 2

Views: 179

Answers (1)

Evk
Evk

Reputation: 101453

You can bind like this:

<CheckBox Name="ChkBox_2"
          AutomationProperties.Name="{Binding RelativeSource={RelativeSource Self}, Path=Content.(AccessText.Text)}">
    <AccessText Name="ChkBox_2AccessText"
                TextWrapping="Wrap">_Option 1</AccessText>
</CheckBox>

Upvotes: 2

Related Questions