【问题标题】:How to remove a control from window programmatically?如何以编程方式从窗口中删除控件?
【发布时间】:2014-08-17 21:03:31
【问题描述】:

我有一个带有按钮的窗口,我需要删除它或者不取决于传递给窗口的参数:

    public MainWindow(bool removeControl)
    {
        InitializeComponent();
        if (removeControl)
        {
            //code to remove the button
        }
    }

在 XAML 文件中,我声明了一个普通按钮:

<Button Width="120" Height="25" Content="Click" Name="ClickButton"></Button>

我知道这可以通过做相反的事情来完成,这意味着根据布尔参数添加按钮,但我需要这样做。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    你可以这样做:

    mybutton.Visibility = Visibility.Collapsed;
    

    ...或者如果您真的希望将其从“逻辑树”中删除...那么这完全取决于 Button 所在的“容器”/父级,以及如何删除它。

    【讨论】:

    • 啊,这工作,我试图修改按钮的 isvisible 属性,但它不起作用,感谢提示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 2011-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多