Khushi
Khushi

Reputation: 1051

Binding a TextBox's Text Property to TextBlock's Attached Property like Grid.Row

Suppose I have two elements. Lets say,

  1. TextBlock
  2. TextBox

Now If I want to bind Text of TextBox to the Text of TextBlock I can do something like :

<TextBox Text="{Binding Text, ElementName=someTextBlock}" />

Now if my TextBlock is inside a Grid as follows:

<Grid>
    <TextBlock Text="someText" Grid.Row=1 Grid.Column=2 />
</Grid>

Now my question is how to bind Text of the TextBox to Grid.Row or Grid.Column?

I mean

<TextBox Text={Binding Grid.Row, ElementName=someTextBlock} />

The above code does not work.

I know I am doing something wrong here.

Upvotes: 1

Views: 690

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564373

You need to use this syntax:

<TextBox Text={Binding Path=(Grid.Row), ElementName=someTextBlock} />

This is documented in Binding.Path:

To bind to an attached property, place parentheses around the attached property. For example, to bind to the attached property DockPanel.Dock, the syntax is Path=(DockPanel.Dock).

For example, this displays 2 rows, with a TextBox containing "1" and a TextBlock below:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <TextBox Text="{Binding Path=(Grid.Row), ElementName=someTextBlock}"></TextBox>
    <TextBlock Grid.Row="1" Name="someTextBlock" Text="Foo" />
</Grid>

Upvotes: 5

Related Questions