关于XAML,我如何检查节点是'TextBox'元素

本文关键字:节点 元素 TextBox 检查 XAML 何检查 关于 | 更新日期: 2023-09-27 18:06:45

我有以下XAML节点

 <TextBox x:Name="myTextContent"
            MinWidth="278"
            Panel.ZIndex="99"
            TabIndex="3"
            IsEnabled="True" Grid.ColumnSpan="2" Margin="47,106,47,247"  
            AutomationProperties.LabeledBy="{Binding ElementName=lblForTextContent}"
            AutomationProperties.AutomationId="myForm_myTextContent"/>

我在另一个类中有一个单元测试,它检查XAML节点是否包含我们在XAML中需要的某些属性(如TabIndex),但该属性检查应该只针对某些元素类型,如TextBox。在单元测试中,我想忽略标签元素。

我如何检查节点是'TextBox'元素?

我想可能是node.NodeType == "TextBox"或者node.NodeType.Equals("TexBox")之类的东西

这里是我的一些代码,我知道这是有效的,但我只需要确保我排除任何不是"文本框"的节点元素,因此我需要知道如何检查节点。NodeType:

XmlAttribute attrTabIndex = node.Attributes["TabIndex"];
                        if (attrTabIndex == null)
                        {
                            if (node.InnerXml.Contains("TabIndex"))
                            {
                                result = true;
                            }
                            else
                            {
                                result = false;
                            }
                        }
                        else
                        {
                            result = true;
                        }

关于XAML,我如何检查节点是'TextBox'元素

您需要的只是xml节点名称:

node.Name == "TextBox"

我知道针对XAML的测试是不同的,但是,作为一个快速测试,我只是创建了一个具有根元素和TextBox节点的XML文档,然后使用下面的代码片段。

这里肯定有更多的代码,但也许这将把你推向正确的方向,你正在尝试做什么。

XmlDocument xdoc = new XmlDocument() { PreserveWhitespace = true };
xdoc.Load("test.xml");
bool result = true;
foreach (var node in xdoc.DocumentElement.ChildNodes.OfType<XmlElement>().Where(n => n.Name == "TextBox"))
{
    result = node.HasAttribute("TabIndex");
}