I am trying to load a Frame on a click of a specific button. I have to recreate an App (from Delphi to C#). This means, I cannot change anything (Anything Layout related). I have a couple of buttons, with icons on top, with a second Grid right under the buttons.
When I start the App, it instantly Loads up the Frame (I am not allowed, to show the frame) Here is my code, to load the frame:
<Grid Margin="0,58,0,0" Grid.ColumnSpan="2">
<Frame Name="Page1" Source="/View/_Test/Page1.xaml"/>
</Grid>
This works, as it should. Next step for me, is to load the frame (and close it by a different button) on click of one of the buttons, shown in the Picture above. Buttons code:
<Button Content="" HorizontalAlignment="Left" Height="25" Margin="217,21,0,0" VerticalAlignment="Top" Width="25" BorderBrush="{x:Null}">
<Button.Background>
<ImageBrush ImageSource="Pictures\bmp00001.bmp" Stretch="Fill"/>
</Button.Background>
</Button>
How do I load and close a frame on click of a button?
CodePudding user response:
In your button click event handler, create the new frame and set it as a child of the grid:
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
Frame f = new Frame();
f.Source = new Uri("https://stackoverflow.com");
YourGrid.Children.Add(f);
}
And on the other button click handler remove it:
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
// Find the element with for example linked extension method
YourGrid.Children.Remove(child);
}
One nice way to find an element is in this answer: https://stackoverflow.com/a/1759923/3279876

