测试TextBox是否存在C#WPF

时间:2018-12-10 13:21:37

标签: c# wpf testing textbox exists

我想测试在代码前面创建的TextBox是否存在。这个TextBox是在“如果测试”中创建的,这就是为什么我要测试它是否存在。但是我不知道如何测试它是否存在,因为我不叫TextBox名称,因为它不存在。

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
            {
                TextBox EpIsolant = new TextBox
                {
                    Width = 100,
                    Height = 29,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    Margin = new Thickness(209, 294, 0, 0)
                };
                MyPage.Children.Add(EpIsolant);

                Label EpIsolantLabel = new Label
                {
                    Content = "Ep isolant (mm)",
                    Width = 100,
                    Height = 29,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    Margin = new Thickness(209, 260, 0, 0)
                };
                MyPage.Children.Add(EpIsolantLabel);
            }
            else 
            {
                // I want to test it here
                // And if it exists, I want to remove it from MyPage.Children
            }
        }

感谢您的帮助!我在Google上找不到任何帮助

PS:当我尝试更改可见性时,它仍然无法正常工作:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TextBox EpIsolant = new TextBox
            {
                Width = 100,
                Height = 29,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(209, 294, 0, 0)
            };
            MyPage.Children.Add(EpIsolant);

            Label EpIsolantLabel = new Label
            {
                Content = "Ep isolant (mm)",
                Width = 100,
                Height = 29,
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                Margin = new Thickness(209, 260, 0, 0)
            };
            MyPage.Children.Add(EpIsolantLabel);
            EpIsolant.Visibility = Visibility.Hidden;
            EpIsolantLabel.Visibility = Visibility.Hidden;

            if (((ComboBoxItem)typeproduit.SelectedItem).Content.ToString() == "Isolant")
            {
                EpIsolant.Visibility = Visibility.Visible;
                EpIsolantLabel.Visibility = Visibility.Visible;
            }
            else
            {
                EpIsolant.Visibility = Visibility.Hidden;
                EpIsolantLabel.Visibility = Visibility.Hidden;
            }
        }

1 个答案:

答案 0 :(得分:0)

如前所述,您可以考虑更改Visibility的{​​{1}}或IsEnabled属性以隐藏/禁用它。

但是,如果您想坚持使用您的解决方案,则可以取出方法外的变量:

Button

然后您可以为其分配一个对象:

private TextBox _epIsolant;

,您还可以检查它是否在您的方法内创建:

_epIsolant = new TextBox
{
    Width = 100,
    Height = 29,
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Top,
    Margin = new Thickness(209, 294, 0, 0)
};