测试文本框是否存在 C# WPF

Test if TextBox Exists C# WPF

我想测试之前在代码中创建的 TextBox 是否存在。这个TextBox是在一个"if test"中创建的,这就是为什么我要测试它是否存在的原因。但是我不知道如何测试它是否存在,因为我无法调用 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;
            }
        }

如前所述,您可以考虑将 ButtonVisibilityIsEnabled 属性 更改为 hide/disable。

但是如果你想坚持你的解决方案,你可以去掉方法外的变量:

private TextBox _epIsolant;

那你可以给它赋一个对象:

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

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

if(_epIsolant == null)
{
  ...
}