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

This may be obvious ... How can I refer to XAML elements later in the same XAML file?

Example:

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

Then I define the various controls inside the grid and want to refer to these lines by name and 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 must Grid.Row="1" change all references to Grid.Row="1" .

EDIT :

Thanks to the answers, I read a little on XAML.

In the end, it is possible to refer to the previous element by name, apparently:

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

or

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

but this does not solve the problem completely, because Grid.Row requires an int, while someGridRow is not an int, this is System.Windows.Controls.RowDefinition.

So what you need is the XAML equivalent

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

which will be written in the code behind

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

or bind Grid.Row to a property in the grid objects that has the path "RowDefinitions.IndexOf" with the someGridRow parameter:

 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 have to do something wrong, although Grid.SetRow works above)

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

 <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 with <x:Reference Name="someGridRow"/> in XAML 2009.

+9
source share
2 answers

For 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> 
+15
source

This, unfortunately, does not work.

The attached properties in question (i.e. Grid.Row) are used by the grid to process your own layout and how to create it, you must specify a number.

Unfortunately, changing numbers when inserting a string is quite common in XAML design. One option is that you can add extra β€œzero-height” lines that are not used, and then use them if you know that you are going to add lines.

+2
source

All Articles