【问题标题】:Test if TextBox Exists C# WPF测试 TextBox 是否存在 C# WPF
【发布时间】:2019-05-11 09:45:51
【问题描述】:

我想测试代码中前面创建的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;
            }
        }

【问题讨论】:

  • 您是否想过只是更改文本框的可见性,而不是根据您的选择一直删除和添加它?
  • 声明方法外的文本框,作为类的“全局”变量?
  • @Fruchtzwerg 是的,我试过了,但它不起作用。 Visibility.Visible 有效,但 Visibility.Hidden 无效,我不明白为什么。

标签: c# wpf testing textbox exists


【解决方案1】:

如前所述,您可以考虑更改ButtonVisibilityIsEnabled 属性以隐藏/禁用它。

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

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)
{
  ...
}

【讨论】:

    猜你喜欢
    • 2013-06-13
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2011-02-16
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多