简体   繁体   中英

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:

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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