简体   繁体   English

如何避免RowDefinition重复?

[英]How to avoid RowDefinition duplication?

I have 15-20 rows, some of them are different but most of them are similar: 我有15-20行,其中一些是不同的,但大多数是相似的:

        <RowDefinition Height="Auto" />

And I want to avoid code duplication, because it's about 10 rows, which goes one by one. 我想避免代码重复,因为它大约有10行,这是一个接一个。 How can it be done? 如何做呢?

It can be done in code without XAML, but I think it is wrong way to solve this problem. 它可以在没有XAML的代码中完成,但我认为这是解决这个问题的错误方法。

I don't think it's possible to somehow shorthand declaration of grid rows, especially when not all of them have the Height property set to "Auto". 我认为不可能以某种方式简化网格行的声明,特别是当并非所有网格行都将Height属性设置为“Auto”时。 You could try using an attached dependency property as demonstrated here . 你可以尝试使用附加依赖属性为证明这里

Please note that this example specifies the number of grid rows and you can see it sets the Height property of each row programmatically. 请注意,此示例指定网格行的数量,您可以看到它以编程方式设置每行的Height属性。 You may have to decide what Height to assign to each row in a more complex way. 您可能必须以更复杂的方式决定要为每行分配的高度。 Although this solution might prove to be more aesthetic in XAML, it may be more complicated to implement and you might have a hard time assigning the proper Height for each row. 虽然这个解决方案可能在XAML中更具美感,但实现起来可能更复杂,并且您可能很难为每一行分配适当的高度。 Personally it seems easier to just declare each row as Visual Studio will allow collapsing the tag and a nice, clear aspect. 就个人而言,只是声明每一行似乎更容易,因为Visual Studio将允许折叠标记和一个漂亮,清晰的方面。

I hope this helps! 我希望这有帮助!

I solved this before by writing a class that offers an attached property to automatically define rows and columns based on a simple comma separated list. 之前我通过编写一个类来解决这个问题,该类提供了一个附加属性,可以根据一个简单的逗号分隔列表自动定义行和列。 What you essentially need to do is create a attached property (I called it AutoRows and define a method for the property-changed metadata. That method parses the value and simply creates new RowDefinition elements. 您基本上需要做的是创建一个附加属性(我称之为AutoRows并为属性更改的元数据定义一个方法。该方法解析该值并简单地创建新的RowDefinition元素。

I used a simple syntax where each value within a comma-separated list equals to a single row. 我使用了一种简单的语法,其中逗号分隔列表中的每个值等于一行。 So 20,20,20 would create 3 rows with the height 20 . 所以20,20,20将创建3行,高度为20 To allow rows with the height auto and also save room, I added a new format to define multiple auto rows in a block. 为了允许高度为auto行并节省空间,我添加了一种新格式来定义块中的多个自动行。 For example 20,~5,20 defines a row with height 20 , then 5 auto-rows and finally another row with height 20 . 例如20,~5,20限定一排的高度20 ,然后5自动行和与高度最后另一行20

My change handler basically does this: 我的变更处理程序基本上是这样

public static void AutoRowsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    RowDefinitionCollection rows = ((Grid)obj).RowDefinitions;
    rows.Clear();

    foreach (string segment in ((string)e.NewValue).Split(','))
    {
        if (segment.StartsWith("~"))
        {
            int count;
            if (!int.TryParse(segment.Substring(1), out count))
                count = 1;

            for (int i = 0; i < count; i++)
                rows.Add(new RowDefinition() { Height = GridLength.Auto });
        }
        else
        {
            GridLengthConverter converter = new GridLengthConverter();
            rows.Add(new RowDefinition() { Height = (GridLength)converter.ConvertFromString(segment) });
        }
    }
}

I left out a few lines that ensured it is parsed correctly and that nothing fails. 我遗漏了几行,确保它被正确解析,没有任何失败。 As you can see, I use the GridLengthConverter to make each non-auto value accept any value that the normal RowDefinition supports. 如您所见,我使用GridLengthConverter使每个非自动值接受正常RowDefinition支持的任何值。 So you can easily join all the row heights by a comma and use it as the value for your attached property. 因此,您可以使用逗号轻松加入所有行高,并将其用作附加属性的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM