在UWP的代码后台中分割网格

3
我该如何分割网格?我是指在设计师中使用RowDefinitions和ColumnDefinitions。以下是我的代码:
composite = (Windows.Storage.ApplicationDataCompositeValue)roamingSettings.Values["enabledDays"];
            ColumnDefinition column = new ColumnDefinition();
            column.Width = new GridLength(20, GridUnitType.Star);
            grid.ColumnDefinitions.Add(column);
            try {
                foreach (KeyValuePair<string, object> MonthBool in composite) {
                    if ((bool)MonthBool.Value) {
                        column.Width = new GridLength(100, GridUnitType.Star);
                        grid.ColumnDefinitions.Add(column);
                    }
                }
            }
            catch (Exception) {
            }

但是当我尝试添加另一列时,会出现“未检测到安装的组件 Element is already the child of another element”的错误。我该怎么办?如何拆分网格?

1个回答

0
所以,我从错误信息中得知我尝试添加同一项(呃),所以我需要做的就是简单地创建新对象:
column = new ColumnDefinition();

所以我的代码现在是这样的:

    composite = (Windows.Storage.ApplicationDataCompositeValue)roamingSettings.Values["enabledDays"];

    ColumnDefinition column = new ColumnDefinition();
    column.Width = new GridLength(20, GridUnitType.Star);
    grid.ColumnDefinitions.Add(column);

    try {
        foreach (KeyValuePair<string, object> MonthBool in composite) {
            if ((bool)MonthBool.Value) {
                column = new ColumnDefinition();
                column.Width = new GridLength(100, GridUnitType.Star);
                grid.ColumnDefinitions.Add(column);
            }
        }
    }
    catch (Exception) {
    }

请问您能否将您的答案标记为已接受,以便后来访问的人更清楚地了解。感谢您的理解。 - Sunteen Wu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接