C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Host a WPF control in a windows application


Posted Date: 08 Aug 2008    Resource Type: Articles    Category: General
Author: Bejaoui BechirMember Level: Gold    
Rating: Points: 10



It is possible to host a Windows presentation foundation control within a given windows form, to know how, I invite you to follow this walkthrough.

First, open Microsoft visual studio 2008, or Sharp Develop 3.0 and create a new WPF user control project.Then copy and paste this XAML code in the XAML code editor


<UserControl x:Class="wpfcontrol.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="207" Width="298" Loaded="UserControl_Loaded">
<Grid Name="myGrid" Height="302" Width="298">
<Label Height="23" Margin="31,32,0,0" Foreground="White" Name="label1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="75">CoordonateX</Label>
<TextBox Height="23" Margin="0,32,11,0" Name="txtX" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
<Label Height="23" HorizontalAlignment="Left" Foreground="White" Margin="31,79,0,0" Name="label2" VerticalAlignment="Top" Width="75">CoordonateY</Label>
<TextBox Height="23" Margin="0,79,11,0" Name="txtY" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
</Grid>
</UserControl>



Now, switch to the design mode, a yellow bar appears at the top of the design mode editor and invites you to click it inorder to reload the control again. Do click the bar and expect until the control is reloaded.

As the control is reloaded, enter the mouse in the control and right click. A context menu appears. As you can remark, there are View code and View XAML. Select view code then click it.

The C# code editor appears. As you remark, the bellow stub is generated

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{

}


In fact, this stub isn't added automatically. It is added from within the XAML code, please review the first four XAML code lines.

<UserControl x:Class="wpfcontrol.UserControl1"
....
......
Loaded="UserControl_Loaded">


In the UserControl tag add the attribute Loaded then press '=', then the intellisense invites you to add an event handler to the loaded event.

Now, implement the loaded stub as follow:

System.Windows.Media.Brush oBrush;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
oColor1 = System.Windows.Media.Colors.Red;
oColor2 = System.Windows.Media.Colors.Black;
oBrush = new System.Windows.Media.LinearGradientBrush(oColor1, oColor2, 20);
this.Background = oBrush;
}


Then add this code to under the loaded event handler


public System.Windows.Media.Color oColor1
{ get; set; }
public System.Windows.Media.Color oColor2
{ get; set; }
public string txtXValue
{
get { return txtX.Text; }
set { txtX.Text = value; }
}
public string txtYValue
{
get { return txtY.Text; }
set { txtY.Text = value; }
}


Now, build the project.
In the other hand, add a new Windows application project to the solution and set it as start up project. To do so, select project menu item then click Set as startup project or in the solution explorer right click the new windows based project and click the 'Set as start up project' context menu item.

Afterward, add a group box in the Form1 and place it at the bottom of the form
then add a button within it. Name this button btnGetCoordonate.
then view the Forom1 code by right clicking it and clicking view code context menu item. The code editor appears then add System.Windows.Forms.Integration to the namespaces list
Do implement the code as follow:

wpfcontrol.UserControl1 oControl;
private void Form1_Load(object sender, EventArgs e)
{
ElementHost oHost = new ElementHost();
oControl = new wpfcontrol.UserControl1();
oHost.Child = oControl;
oHost.Dock = DockStyle.Fill;
this.Controls.Add(oHost);
oHost.Visible = true;
}

private void btnGetCoordonate_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format("X: {0},Y: {1}",oControl.txtXValue,oControl.txtYValue));
}


Now build the entire solution and run the project. It the solution doesn't lunched correctly then verifiy whether the windows project is set as start up project. That's it

Good Dotneting!!!





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
WPF Control  .  Windows  .  ElementHost  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Intersect and Inner join
Previous Resource: Introduction to Share point 2007
Return to Discussion Resource Index
Post New Resource
Category: General


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use