Reputation: 1453
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.
Upvotes: 9
Views: 7003
Reputation: 61
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.
Upvotes: 0
Reputation: 185170
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>
Upvotes: 16
Reputation: 564701
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.
Upvotes: 2