Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open User Control in a grid in main Window - WPF

I'm writing now an app with WPF and trying to remember things I did long time ago. I'm trying to have a window with 4 buttons that I will navigate with them through the app.

My code:

<Grid>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="55*"/>
            <ColumnDefinition Width="453*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <StatusBar Margin="0" VerticalAlignment="Top" Height="23" Grid.ColumnSpan="4" Grid.Row="2" Background="#FF1A202C"/>
        <Menu Margin="0" Height="23" Grid.ColumnSpan="4" Background="#FF1A202C" />
        <StackPanel Grid.Row="1" Width="224.2" HorizontalAlignment="Left" Background="#FF1F7872"  >
            <Button Content="Dashboard" Style="{StaticResource BorderlessButton}" Foreground="#FFF1E7E7" Template="{DynamicResource GlassButton}" Margin="25,20,23,20" Click="Button_Click_1" />
            <Button Content="Components" Style="{StaticResource BorderlessButton}" Foreground="#FFF1E7E7" Template="{DynamicResource GlassButton}" Margin="25,20,23,20"/>
            <Button Content="TimeLine" Style="{StaticResource BorderlessButton}" Foreground="#FFF1E7E7" Template="{DynamicResource GlassButton}" Margin="25,20,23,20"/>
            <Button Content="Drilldown" Style="{StaticResource BorderlessButton}" Foreground="#FFF1E7E7" Template="{DynamicResource GlassButton}" Margin="25,20,23,20"/>
        </StackPanel>
        <Grid Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Grid.ColumnSpan="2" Background="#FF72B095">

        </Grid>     
    </Grid>

I've 4 user controls that I want they will appear in the Grid (maybe I'll need to replace the control) when I'm clicking the buttons.

A friend suggest that I'll use storyboard on Blend, but I remembered that a long time ago I did it differently and I used only XAML and C#.

I tried to use the storyboard tool but I didn’t understand how to use it, in general I think the storyboard tool is overkill for my use.

like image 649
SigmaOmega Avatar asked Dec 13 '25 10:12

SigmaOmega


1 Answers

Give a name to the grid:

<Grid  Name="MainGrid" >

</Grid>

and then, in code behine, handle click events:

void Button_Click_1(object sender, MouseEventArgs e)
{ 
    MainGrid.Children.Add( // Your control //);
}
like image 154
Ramin Avatar answered Dec 16 '25 16:12

Ramin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!