XAML: Can I refer to a Grid row or column by name?

2020-06-23 07:13发布

问题:

If I name each row and column in a grid, can I set a control's grid.row="Row_Top"? I have defined StaticResources to refer to it but is there a converter or some other method to accomplish this without the resource?

回答1:

As far as I know there's no built-in way to do it... however, you could use markup extensions to retrieve a row or column by its name :

[MarkupExtensionReturnType(typeof(int))]
public abstract class GridBandExtensionBase : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (serviceProvider != null)
        {
            var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (target != null)
            {
                var obj = target.TargetObject as DependencyObject;
                if (obj != null)
                {
                    var grid = VisualTreeHelper.GetParent(obj) as Grid;
                    if (grid != null)
                    {
                        return GetBandIndex(grid);
                    }
                }
            }
        }
        return 0;
    }

    protected abstract int GetBandIndex(Grid grid);
}

public class GridRowExtension : GridBandExtensionBase
{
    public GridRowExtension() {}

    public GridRowExtension(string rowName)
    {
        this.RowName = rowName;
    }

    [ConstructorArgument("rowName")]
    public string RowName { get; set; }

    protected override int GetBandIndex(System.Windows.Controls.Grid grid)
    {
        for (int i = 0; i < grid.RowDefinitions.Count; i++)
        {
            if (grid.RowDefinitions[i].Name == RowName)
            {
                return i;
            }
        }
        return 0;
    }
}

public class GridColumnExtension : GridBandExtensionBase
{
    public GridColumnExtension() {}

    public GridColumnExtension(string columnName)
    {
        this.ColumnName = columnName;
    }

    [ConstructorArgument("columnName")]
    public string ColumnName { get; set; }

    protected override int GetBandIndex(System.Windows.Controls.Grid grid)
    {
        for (int i = 0; i < grid.ColumnDefinitions.Count; i++)
        {
            if (grid.ColumnDefinitions[i].Name == ColumnName)
            {
                return i;
            }
        }
        return 0;
    }
}

You could then use these extensions as follows :

<Button Grid.Row="{my:GridRow Row_Top}"
        Grid.Column="{my:GridColumn Column_Right}"
        Content="Hello world" />

NOTE: if you compile these markup extensions in the same assembly as your application, the designer won't be able to call the constructor with a parameter (but it will work fine at runtime). This is a known bug that Microsoft doesn't intend to fix. If you want it to work at design time, put the markup extension in a separate assembly, or specify the RowName/ColumnName properties explicitly, as follows :

<Button Grid.Row="{my:GridRow RowName=Row_Top}"
        Grid.Column="{my:GridColumn ColumnName=Column_Right}"
        Content="Hello world" />