Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a row/column definition in Grid.Row/Grid.Column?

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.

like image 509
SemMike Avatar asked Jun 22 '11 23:06

SemMike


People also ask

When referring to a grid what is a row?

A grid row is a horizontal track in a CSS Grid Layout, that is the space between two horizontal grid lines. It is defined by the grid-template-rows property or in the shorthand grid or grid-template properties.

What is grid row and grid column?

A grid is a set of intersecting horizontal and vertical lines defining columns and rows. Elements can be placed onto the grid within these column and row lines.

How do you set the number of rows and columns in a grid?

Show activity on this post. You can use grid-template-columns to specify the number of columns. The number of columns is defined by the number of values in the list. Below, I'm using repeat() as shorthand to generate four values.

What is 1FR in CSS grid?

With CSS Grid Layout, we get a new flexible unit: the Fr unit. Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.


2 Answers

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>
like image 105
H.B. Avatar answered Sep 23 '22 02:09

H.B.


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.

like image 26
Reed Copsey Avatar answered Sep 26 '22 02:09

Reed Copsey