9

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
  • I think all that would never work as the property path does not allow the invocation of methods like IndexOf.
    – brunnerh
    Commented Jun 23, 2011 at 12:35

3 Answers 3

16

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>
7
  • Nice way to work around this limitation via MarkupExtensions. Commented Jun 23, 2011 at 0:24
  • Thanks a lot! I didn't know about markup extensions or type converters at all, I am reading up on them now on MSDN. I think I will actually use this kind of thing (not just "for the lulz"), it makes me like XAML better already...
    – SemMike
    Commented Jun 23, 2011 at 1:04
  • @SemMike: I think that i should mention that this might have some performance impact on larger grids as the Reference lookup behavior is rather complex and the number of the rows is retrieved via IndexOf which depending on implementation may also be slow. This is why my answer is prefixed with that line.
    – brunnerh
    Commented Jun 23, 2011 at 2:08
  • @SemMike: Just had a look, according to a decompiler IndexOf should have O(1), one should probably not prematurely think about optimization here i guess.
    – brunnerh
    Commented Jun 23, 2011 at 2:14
  • Thanks, I will probably use your method in the end but I am curious about XAML now, I did some reading and edited the original question...
    – SemMike
    Commented Jun 23, 2011 at 4:27
2

This, unfortunately, doesn't work.

The attached properties in question (ie: Grid.Row) are used by grid to handle their own layout, and the way it's designed, you have to put in the number.

Unfortunately, changing the numbers when inserting a row is pretty common in XAML development. One option - You can put in extra "zero height" rows that are unused, and later use them, if you know you're going to be adding rows.

5
  • Thanks. Why isn't such a simple thing possible? Something like Grid.Row = "{x:Name someGridRow}" should be implemented...
    – SemMike
    Commented Jun 22, 2011 at 23:56
  • @SemMike: I suspect it's because of how this works. Grid investigates its children for Grid.Row, and there's no real binding involved... Any naming would have to be done off non-type safe identifiers, and could lead to weird issues. Commented Jun 23, 2011 at 0:01
  • Well, with Name="someName" you can already reference the item in code-behind, so why not in the XAML itself? It's not less "type safe" in XAML than it is in C#... anyway it's not possible, so the point is moot, thanks for putting me out of my misery quickly!
    – SemMike
    Commented Jun 23, 2011 at 0:14
  • sorry, I had to make the other guy's answer the accepted one! Still, it's not possible in vanilla XAML (surprising) so you're right.
    – SemMike
    Commented Jun 23, 2011 at 1:06
  • @SemMike: Yeah, no problem. His markup extension is a great way to work around it. Commented Jun 23, 2011 at 1:19
0

Another way to get the Grid.Column would be binding to the ViewModel like this:

<TextBlock Text="Nummer: " Grid.Column="{Binding Cols.C1}" />

In the ViewModel:

public ColumnDefs Cols { get; }

And finally the definition of your ColumnWidth values:

public class ColumnDefs
{
    public int C1 => 0; // or however you retrieve the columns index
    public int C2 => 1;
}

Know, if you have to insert columns (works with rows also), you just have to change the index values in your ViewModel. This approach should be less exhaustive than changing every Grid.Column attribute in your xaml.

Not the answer you're looking for? Browse other questions tagged or ask your own question.