简体   繁体   English

如何以编程方式从窗口中删除控件?

[英]How to remove a control from window programmatically?

I have a window with a button in it, and I need to remove it OR not depending on the argument passed to the window: 我有一个带有按钮的窗口,我需要删除它,或者不取决于传递给窗口的参数:

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

In the XAML file I declare a normal button: 在XAML文件中,我声明一个普通按钮:

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

I know this can be done by doing the reverse thing which means add the button depending of the Boolean parameter, but I need to do so. 我知道这可以通过做相反的事情来完成,这意味着根据Boolean参数添加按钮,但是我需要这样做。

You can do: 你可以做:

mybutton.Visibility = Visibility.Collapsed;

...or if you really want it to be removed from the "logical tree"...then it all depends what "container"/parent that Button is in, in how you remove it. ...或者如果您确实希望将其从“逻辑树”中删除...那么,这全都取决于Button所在的“容器” /父级以及如何删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM