WPF View Switcher Tutorial
If you are new to WPF ( Windows Presentation Foundation ) please refer the below items.
Dynamic Switching of views in WPF using UserControl and Grid Layout.
- 1. Controls and Layouts used
- 2. Step by Step approach for creating the project
- 3. MainWindow Code Explanation
- 4. RedView Code Explanation
- 5. GreenView Code Explanation
1. Controls and Layouts used
Views – UserContol
ViewHolder – Grid
2. Step by Step approach for creating the project
- Launch Visual Studio.
- From the menu bar select File, New Project.
- Select the WPF Project Template
- Name your new application SwitchingViews, and then click OK.
- Rename Window1.xaml to MainWindow.xaml find and replace all the occurrences of Window1.xaml to MainWindow.xaml.
- Follow MainWindow Code Explanation section for modifying the code.
- Add User Control
- Name User Control RedView, and then click OK.
- Follow RedView Code Explanation section for modifying the code .
- 10 .For creating GreenView repeat step 8 and 9.
3. MainWindow Code Explanation
The gridMain is main Grid which hosts the ‘View Holder ‘ Grid (gridViewHolder). On clicking the Red/Green Button we can switch the Default view to Red/Green View.The void ChangeViewTo(VIEWTYPE viewType) method clears the child/existing view to the specified viewType and Adds the new View.
MainWindow.xaml
<Grid Background="Black" Name="gridMain">
<Grid.RowDefinitions>
<RowDefinition Height="46-" />
<RowDefinition Height="213-" />
</Grid.RowDefinitions>
<Button Background="Red"
HorizontalAlignment="Left"
Margin="24,12,0,11"
Width="75" Height ="22"
Name="btnRedView"
Content="Red View"
Click="btnRedView_Click">
</Button>
<Button Background="Green"
HorizontalAlignment="Left"
Margin="105,12,0,11"
Width="75" Height ="22"
Name="btnGreenView"
Content="Green View"
Click="btnGreenView_Click">
</Button>
<Button Background="Gold"
HorizontalAlignment="Left"
Margin="348,13.21,0,12.21"
Width="75"
Name="bnClear"
Content="Clear Holder"
Click="bnClear_Click">
</Button>
<Button HorizontalAlignment="Right"
Margin="0,13.21,20,12.21"
Width="75"
Name="btnCancel"
Content="Cancel"
Click="btnCancel_Click">
</Button>
<!--View Holder Grid -->
<Grid Background="Gray"
Grid.Row="1"
Name="gridViewHolder">
<Label FontSize="15"
FontWeight="bold"
Margin="217,85,202,98"
Name="lblViewHolder"
Content="View Holder">
</Label>
</Grid>
</Grid>
</Window>
4. RedView Code Explanation
The void AdjustSize()Makes the size of the UserControl Adjustable to the size of view holder Grid at Runtime.
RedView.xaml
<UserControl x:Class="SwitchingViews.RedView"
xmlns="[http://schemas.microsoft.com/winfx/2006/xaml/presentation"](http://schemas.microsoft.com/winfx/2006/xaml/presentation");
xmlns:x="[http://schemas.microsoft.com/winfx/2006/xaml"](http://schemas.microsoft.com/winfx/2006/xaml");
Height="300" Width="300" Background="Red">
<Grid><Label VerticalAlignment="Top"
FontSize="15"
FontWeight="bold"
Margin="115,35,109,0"
Height="29"
Name="lblViewHolder"
Content="Red View">
</Label> </Grid>
</UserControl>
5. GreenView Code Explanation
Same as RedView.