Reader Level:
Articles

WebBrowser Control in WPF

By Rahul Saxena on April 08, 2010
In this article, I am going to show how we can use WebBrowser control in WPF application.
  • 0
  • 0
  • 42462
Download Files:
 

In this article, I am going to show how we can use WebBrowser control to show html document within our WPF application. WPF introduces WebBrowser control to ensure that we can show html pages embedded inside the control. Here I use 2 buttons also to go back and forward.

This is my XAML code

<Window x:Class="WebBrowserControlIn_WPF.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="WebBrowser Control in WPF" Height="Auto" Width="Auto" Loaded="Window_Loaded">

    <Grid x:Name="LayoutRoot" Width="Auto">

        <Grid.RowDefinitions>

            <RowDefinition Height="10"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="65"></ColumnDefinition>

            <ColumnDefinition Width="*"></ColumnDefinition>

            <ColumnDefinition Width="10"></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch">

            <WebBrowser x:Name="myBrowser" Margin="0,0,0,0" Cursor="Arrow" Height="Auto"/>

        </Grid>

        <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch">

            <Grid.RowDefinitions>

                <RowDefinition Height="40"/>

                <RowDefinition Height="40"/>

            </Grid.RowDefinitions>

            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="0"  VerticalAlignment="Stretch">

                <Border Height="20" BorderThickness="2" CornerRadius="5,5,0,0" VerticalAlignment="Top"   />

                <Button Click="GoBack_Click" Width="50" Height="20" x:Name="GoBack">Go Back</Button>

            </Grid>

 

            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1"  VerticalAlignment="Stretch">

                <Button Click="GoForward_Click" Width="61" Height="20" x:Name="GoForward">Go Forward</Button>

            </Grid>

        </Grid>

    </Grid>

</Window>

This is my XAML.cs code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace WebBrowserControlIn_WPF

{

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

        }

 

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            myBrowser.Navigate(new Uri("http://www.c-sharpcorner.com"));

        }

 

        private void GoBack_Click(object sender, RoutedEventArgs e)

        {

            if (myBrowser.CanGoBack)

            {

                myBrowser.GoBack();

            }

            else

            {

                MessageBox.Show("Cannot Go back");

            }

        }        

 

        private void GoForward_Click(object sender, RoutedEventArgs e)

        {

            if (myBrowser.CanGoForward)

            {

                myBrowser.GoForward();

            }

            else

            {

                MessageBox.Show("Cannot Go Forward");

            }

        }

    }

}


When we run the application then...

WPFWEBBrowser.JPG

Image 1.

Rahul Saxena

Rahul K Saxena is working as a Technical Analyst in Noida, India. He is Masters in Computers Application.Here in the IT industry for more than 6+ years and his main technical skills include SharePoint 2010, MOSS 2007, C#... Read more

COMMENT USING

Trending up