3

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.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
SigmaOmega
  • 839
  • 1
  • 10
  • 13

1 Answers1

3

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 //);
}
Ramin
  • 2,133
  • 14
  • 22
  • so easy, I'm embarrassed. 10x alot! – SigmaOmega Dec 02 '12 at 09:33
  • I've a little Q, in the code behind I need always to create a new instance of my control? like this: `MainGrid.Children.Clear(); Dashboard board = new Dashboard(); MainGrid.Children.Add(board);` because when I'm trying to do like this: `MainGrid.Children.Clear(); MainGrid.Children.Add(Dashboard );` I'm getting an error :( – SigmaOmega Dec 02 '12 at 12:08
  • No, if you have called `InitializeComponent()` method, an instance of MainGrid is created. – Ramin Dec 03 '12 at 06:18