6

I am very new to wpf. How can I implement CardLayout functionality from java? I have a window where I need completely switch contents depending on user actions, like different tabs in tabbed pane.

michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

1 Answers1

4

You can create multiple pages and host them in a frame. Look here for more information.

XAML:

<Window x:Class="CardLayout"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CardLayout" Height="300" Width="300">
    <Grid>
        <Frame Height="200" HorizontalAlignment="Left" Margin="12,40,0,0" Name="frame1" VerticalAlignment="Top" Width="254" NavigationUIVisibility="Hidden" />

        <ComboBox HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="254" SelectedIndex="0" SelectionChanged="comboBox1_SelectionChanged">
            <ComboBoxItem>FirstPage</ComboBoxItem>
            <ComboBoxItem>SecondPage</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

Code Behind:

public partial class CardLayout : Window
{
    private Page[] pages = new Page[] {new Page1(), new Page2()};

    public CardLayout()
    {
        InitializeComponent();
    }

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        frame1.Content = pages[((ComboBox) sender).SelectedIndex];
    }
}
fardjad
  • 20,031
  • 6
  • 53
  • 68
  • 1
    I know it's old but appears first in Google for 'CardLayout WPF': there is also `TabControl` which is really, really useful (at least for me). – mwilczynski Aug 17 '14 at 20:08