Nerdynosaur
Nerdynosaur

Reputation: 1888

How to return using the Usercontrol WPF

Basically i have a Main Window page and a Setting page which is the User Control. When i click the Setting button on the main window, the Grid in the main page will show the setting page user control.

Meanwhile, there is a Cancel button on the setting page. When i click on it, the setting page user control will disappear from the Grid and return how it is at the main window. So what can i write in my user control code? Or any other ways? Thanks.

My main window:

<Grid Name="grid1"> 
  <TextBlock  Text="Hello World" />
  <TextBlock Text="Welcome" />
  <Button Name="settingBtn" Content="Setting" Click="settingBtn_Click" />   
</Grid>

My main window code cs:

 private void notifyBtn_Click(object sender, RoutedEventArgs e)
    {

        UserControls.NotifySetting Set = new UserControls.NotifySetting();
        grid1.Children.Clear();
        grid1.Children.Add(Set);

    }

My Usercontrol page:

<Grid > 
  <RadioButton Name="r1" Content="Red Color Font" />
  <RadioButton Name="r3" Content="Blue Color Font" />
  <Button Name="saveBtn" Content="Save" Click="saveBtn_Click" />  
  <Button Name="cancelBtn" Content="Cancel" Click="cancelBtn_Click" />  
</Grid>

Upvotes: 0

Views: 1661

Answers (1)

sam1589914
sam1589914

Reputation: 816

You can put this in the cancel button click event:

 (this.Parent as Panel).Children.Remove(this)

Upvotes: 2

Related Questions