Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6062447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:03:10+00:00 2026-05-23T09:03:10+00:00

This may be obvious… How do I reference XAML elements later in that same

  • 0

This may be obvious… How do I reference XAML elements later in that same XAML file?

Example:

<Grid.RowDefinitions>
    <RowDefinition Height="661*" Name="someGridRow" />
    <RowDefinition Height="230*" Name="someOtherGridRow"/>
</Grid.RowDefinitions>

Then I define various controls inside the grid and I’d like to reference these rows by name, not by number:

<RichTextBox Grid.Row="someGridRow" ... />

Because if I use Grid.Row="0" on many controls, then when I add a row before the first row, I have to change all the references to Grid.Row="1" by hand.

EDIT:

Thanks to the answers I have been reading a bit on XAML.

After all, it IS possible to reference a previous element by name apparently:

Grid.Row="{Binding ElementName=someGridRow}"

or

Grid.Row="{x:Reference someGridRow}"

but this doesn’t solve the problem entirely because Grid.Row requires an int, whereas someGridRow is not an int, it’s a System.Windows.Controls.RowDefinition.

So what is needed is the XAML equivalent of

Grid.Row = grid.RowDefinitions.IndexOf(someGridRow)

which in code behind would be written

Grid.SetRow(richTextBox, grid.RowDefinitions.IndexOf(someGridRow))

or do a binding of Grid.Row to the property, on the object grid, which has the path "RowDefinitions.IndexOf" with the parameter someGridRow:

PropertyPath path = new PropertyPath("RowDefinitions.IndexOf", someGridRow);
Binding binding = new Binding() { ElementName = "grid", Path = path };
richTextBox.SetBinding(Grid.RowProperty, binding);

(this actually doesn’t work in C#, so I must be doing something wrong, although Grid.SetRow above does work)

XAML 2009 defines <x:Arguments> to invoke constructors which have parameters. If that worked in WPF XAML, then something like that would work I suppose?

<Grid.Row>
  <Binding ElementName="grid">
    <Binding.Path>
      <PropertyPath>
        <x:Arguments>
          RowDefinitions.IndexOf
          <Binding ElementName="someGridRow"/>
        </x:Arguments>
      </PropertyPath>
    </Binding.Path>
  </Binding>
</Grid.Row>

where <Binding ElementName="someGridRow"/> can also be replaced by <x:Reference Name="someGridRow"/> in XAML 2009.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:03:11+00:00Added an answer on May 23, 2026 at 9:03 am

    For the lulz:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Markup;
    using System.Windows.Controls;
    using System.Windows;
    
    namespace Test.MarkupExtensions
    {
        class GridDefinitionExtension : MarkupExtension
        {
            public string Name { get; set; }
    
            public GridDefinitionExtension(string name)
            {
                Name = name;
            }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                var refExt = new Reference(Name);
                var definition = refExt.ProvideValue(serviceProvider);
                if (definition is DefinitionBase)
                {
                    var grid = (definition as FrameworkContentElement).Parent as Grid;
                    if (definition is RowDefinition)
                    {
                        return grid.RowDefinitions.IndexOf(definition as RowDefinition);
                    }
                    else
                    {
                        return grid.ColumnDefinitions.IndexOf(definition as ColumnDefinition);
                    }
                }
                else
                {
                    throw new Exception("Found object is neither a RowDefinition nor a ColumnDefinition");
                }
            }
        }
    }
    
    <Grid Width="200" Height="200"
          xmlns:me="clr-namespace:Test.MarkupExtensions">
        <Grid.RowDefinitions>
            <RowDefinition Name="row1" />
            <RowDefinition Name="row2" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1" />
            <ColumnDefinition Name="col2" />
        </Grid.ColumnDefinitions>
        <Border Background="Lime" Grid.Row="{me:GridDefinition row1}" Grid.Column="{me:GridDefinition col1}" />
        <Border Background="Red" Grid.Row="{me:GridDefinition row2}" Grid.Column="{me:GridDefinition col1}" />
        <Border Background="Yellow" Grid.Row="{me:GridDefinition row1}" Grid.Column="{me:GridDefinition col2}" />
        <Border Background="Blue" Grid.Row="{me:GridDefinition row2}" Grid.Column="{me:GridDefinition col2}" />
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem obvious to most people, but I'm just trying to confirm that
Being that my C++ isn't that great, this may be a really simple/obvious answer,
i'm a long-time newbie to c#, and this question may be too obvious, so
This may be obvious but it's been ages since I used flash. I have
This may be obvious to some, but I've been wondering: why should I depend
This may be obvious but I can't see to find my way to the
This may be obvious but I can't figure out how to bind a static
This may be something obvious but I've been banging my head against it for
This may be really obvious, but in rails how can I get the contents
This may seem like an obvious answer, but I can't seem to find an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.