简体   繁体   English

如何遍历 uwp 页面中的每个元素?

[英]How to iterate through each of an element in a uwp page?

I want to validate if any of the comboBoxes on the page have not been selected.我想验证是否未选择页面上的任何组合框。

I have tried:我试过了:

foreach (ComboBox combo in PageName)
{
    if (combo.SelectedIndex == -1)
    {
        // Activate Teaching Tip assosiated with this error to tell the user what needs changing
        NotSelected.IsOpen = true;
        return;
    }
}

However it says 'PageName' is a type which isn't valid in the given context.但是它说“PageName”是一种在给定上下文中无效的类型。

Is there a way to achieve this?有没有办法实现这一目标?

You need to reference a panel which contains ComboBoxes:您需要引用一个包含 ComboBox 的面板:

foreach (UIElement item in grid.Children)
{
    // ...
}

Inside foreach loop check if an item is ComboBox then cast it to a ComboBox to perform some operation on it:在 foreach 循环内部检查项目是否为 ComboBox,然后将其转换为 ComboBox 以对其执行一些操作:

if (item is ComboBox)
{
    ComboBox comboBox = item as ComboBox;
    if (comboBox.SelectedIndex == -1)
    {
        // ...
    }
}

Place that teaching tip somewhere on the UI and hide it.将该教学提示放置在 UI 上的某个位置并将其隐藏。 Make it visible when item is not selected in a ComboBox:在 ComboBox 中未选择项目时使其可见:

foreach (UIElement item in grid.Children)
{
    if (item is ComboBox)
    {
        ComboBox comboBox = item as ComboBox;
        if (comboBox.SelectedIndex == -1)
            teachingTip.Visibility = Visibility.Visible;
    }
}

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

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