簡體   English   中英

關於XAML如何檢查節點是否為“ TextBox”元素?

[英]In regards to XAML How do I check that the node is 'TextBox' element?

我有以下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")

這是我知道可以正常工作的一些代碼,但是我只需要確保排除不屬於“文本框”的所有節點元素,因此我需要知道如何檢查node.NodeType:

XmlAttribute attrTabIndex = node.Attributes["TabIndex"];
                        if (attrTabIndex == null)
                        {

                            if (node.InnerXml.Contains("TabIndex"))
                            {
                                result = true;
                            }
                            else
                            {
                                result = false;
                            }
                        }
                        else
                        {
                            result = true;
                        }

您需要的只是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");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM