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

前端 未结 2 1290
情深已故
情深已故 2020-12-28 22:27

This may be obvious... How do I reference XAML elements later in that same XAML file?

Example:


    

        
2条回答
  •  粉色の甜心
    2020-12-28 23:07

    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");
                }
            }
        }
    }
    
    
        
            
            
        
        
            
            
        
        
        
        
        
    
    

提交回复
热议问题