tbiro.com

Forums 
   HackPerfCounter
   RADPass
   SHEdit
   Toys
 
   Contact me
   My PGP Key
 

Tabbed wizard with fade in and fade out


How to get this code going: Create a Windows Presentation Foundation project named TabbedWizardSample and paste the following code in the specified file.


Paste this code into Window1.xaml:

<Window x:Class="TabbedWizardSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="305" Width="479">
    <Grid>
        <TabControl Margin="16,21,18,42" Name="tabControl1">
            <TabItem Header="Step1">
                <Label>Step1 controls</Label>
            </TabItem>
            <TabItem Header="Step2">
                <Label>Step2 controls</Label>
            </TabItem>
            <TabItem Header="Step3">
                <Label>Step3 controls</Label>
            </TabItem>
            <TabItem Header="Step4">
                <Label>Step4 controls</Label>
            </TabItem>
        </TabControl>
        <Button Name="back" Height="30" Margin="16,0,0,10" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="136" Click="back_Click">Back</Button>
        <Button Name="next" Height="30" Margin="0,0,18,10" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="137" Click="next_Click">Next</Button>

    </Grid>
</Window>



Paste this code into Window1.xaml.cs:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace TabbedWizardSample
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            tabControl1.SelectedIndex = 0;

            SetVisibility();
        }

        private void SetVisibility()
        {
            for (Int32 index = 0; index < tabControl1.Items.Count; index++)
            {
                TabItem tabItem = tabControl1.Items[index] as TabItem;

                if (index <= tabControl1.SelectedIndex)
                {
                    tabItem.Visibility = Visibility.Visible;
                }
                else
                {
                    tabItem.Visibility = Visibility.Hidden;
                }
            }

            if (tabControl1.SelectedIndex == 0)
            {
                back.Visibility = Visibility.Hidden;
            }
            else
            {
                back.Visibility = Visibility.Visible;
            }

            if (tabControl1.SelectedIndex == tabControl1.Items.Count - 1)
            {
                next.Visibility = Visibility.Hidden;
            }
            else
            {
                next.Visibility = Visibility.Visible;
            }
        }

        void anim_Completed(object sender, EventArgs e)
        {
            // avoid ghost tabs
            SetVisibility();
        }

        private void next_Click(object sender, RoutedEventArgs e)
        {
            tabControl1.SelectedIndex++;

            TabItem tabItem = tabControl1.Items[tabControl1.SelectedIndex] as TabItem;

            tabItem.Opacity = 0;
            tabItem.Visibility = Visibility.Visible;

            DoubleAnimation anim = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(500));

            anim.Completed += new EventHandler(anim_Completed);

            tabItem.BeginAnimation(TabItem.OpacityProperty, anim);
        }

        private void back_Click(object sender, RoutedEventArgs e)
        {
            TabItem tabItem = tabControl1.Items[tabControl1.SelectedIndex] as TabItem;

            tabControl1.SelectedIndex--;

            tabItem.Opacity = 1;
            tabItem.Visibility = Visibility.Visible;

            DoubleAnimation anim = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(500));

            anim.Completed += new EventHandler(anim_Completed);

            tabItem.BeginAnimation(TabItem.OpacityProperty, anim);
        }


    }
}

Compile and run.


Comment in the forum





Sitemap generated by Sitemap Manager