【问题标题】:Split grid in UWP code behind在 UWP 代码中拆分网格
【发布时间】:2017-04-29 17:15:14
【问题描述】:

如何拆分网格?我的意思是像在具有 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) {
            }

但是当它尝试添加另一列时,我收到错误“未检测到已安装的组件元素已经是另一个元素的子元素”。我能做些什么?如何分割网格?

【问题讨论】:

    标签: c# uwp


    【解决方案1】:

    所以,我从错误消息中发现我尝试添加相同的项目(duh),所以我需要做的只是简单地创建新对象:

    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) {
        }
    

    【讨论】:

    • 您能否将您的答案标记为已接受,以便以后访问此内容的人更清楚。感谢理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2016-02-07
    • 2020-01-10
    相关资源
    最近更新 更多