这可能很明显......我以后如何在同一个 XAML 文件中引用 XAML 元素?
例子:
<Grid.RowDefinitions>
<RowDefinition Height="661*" Name="someGridRow" />
<RowDefinition Height="230*" Name="someOtherGridRow"/>
</Grid.RowDefinitions>
然后我在网格内定义了各种控件,我想按名称引用这些行,而不是按编号:
<RichTextBox Grid.Row="someGridRow" ... />
因为如果我使用
Grid.Row="0"
在许多控件上,然后当我在第一行之前添加一行时,我必须将所有引用更改为 Grid.Row="1"
用手。编辑 :
感谢我在 XAML 上阅读了一些答案。
毕竟,显然可以通过名称引用先前的元素:
Grid.Row="{Binding ElementName=someGridRow}"
或者
Grid.Row="{x:Reference someGridRow}"
但这并不能完全解决问题,因为 Grid.Row 需要一个 int,而 someGridRow 不是一个 int,它是一个 System.Windows.Controls.RowDefinition。
所以需要的是 XAML 等价物
Grid.Row = grid.RowDefinitions.IndexOf(someGridRow)
后面的代码会写成
Grid.SetRow(richTextBox, grid.RowDefinitions.IndexOf(someGridRow))
或绑定(bind)
Grid.Row
到属性,在对象 grid
,其路径为 "RowDefinitions.IndexOf"
带参数someGridRow
:PropertyPath path = new PropertyPath("RowDefinitions.IndexOf", someGridRow);
Binding binding = new Binding() { ElementName = "grid", Path = path };
richTextBox.SetBinding(Grid.RowProperty, binding);
(这实际上在 C# 中不起作用,所以我一定做错了,尽管上面的
Grid.SetRow
确实有效)XAML 2009 定义
<x:Arguments>
调用具有参数的构造函数。如果这在 WPF XAML 中有效,那么我想这样的方法会有效吗?<Grid.Row>
<Binding ElementName="grid">
<Binding.Path>
<PropertyPath>
<x:Arguments>
RowDefinitions.IndexOf
<Binding ElementName="someGridRow"/>
</x:Arguments>
</PropertyPath>
</Binding.Path>
</Binding>
</Grid.Row>
在哪里
<Binding ElementName="someGridRow"/>
也可以替换为 <x:Reference Name="someGridRow"/>
在 XAML 2009 中。
最佳答案
对于 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>
关于wpf - 如何在 Grid.Row/Grid.Column 中引用行/列定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6447777/