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.