How do I bind a string array with a ListBox?

A ListBox can be bound with a string array using the ItemsSource property of the ListBox control. The following code snippet is used to bind a string array with the ListBox. [XAML] <Window x:Class=’WpfApplication4.Window1′ xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’ Title=’Window1′ Height=’300′ Width=’300′ Loaded=’Window_Loaded’> <Grid> <ListBox Name=’LB2’/> </Grid> </Window> [C#] private void Window_Loaded(object sender, RoutedEventArgs e) { string[] lbitems = { ‘Item1’, ‘Item2’, ‘Item3’, ‘Item4’ }; LB2.ItemsSource = lbitems; }

How placing controls in a WrapPanel differs from a Grid?

Placing controls inside a WrapPanel will shrink the control to it’s minimum width or it will wrap the controls to the next line when the form’s size is reduced. But when the form’s size is increased it will not resize the control accordingly, instead, it will place the controls next to each other. On the other hand, if the controls are placed inside a Grid, it will resize the controls when the form is resized.

How do I print a form?

Unfortunately, there is no easy way to print a form. You may implement this function with the steps given below. 1. Add a print function to your application. To do this, you should add a PrintDocument component to your application. Please drag a PrintDocument from the toolbox to your form. After that, you should create a PrintDialog and add the code to print the document. [C#] private void buttonPrint_Click(object sender, EventArgs e) { PrintDialog printDialog1 = new PrintDialog(); printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog(); if (result == DialogResult.OK) printDocument1.Print(); } 2. Draw the form when printing. This step is a little complex. You should handle the PrintPage of the printDocument1 and draw the form to the printer device. In the event you may copy the form to an image and then draw it to the printer device. [C#] using System.Drawing.Printing; private void printDocument1_PrintPage( object sender, PrintPageEventArgs e) { Graphics graphic = CreateGraphics(); Image memImage = new Bitmap( Size.Width, Size.Height, graphic ); Graphics memGraphic = Graphics.FromImage( memImage ); IntPtr dc1 = graphic.GetHdc(); IntPtr dc2 = memGraphic.GetHdc(); BitBlt( dc2, 0, 0, ClientRectangle.Width, ClientRectangle.Height, dc1, 0, 0, 13369376 ); graphic.ReleaseHdc( dc1 ); memGraphic.ReleaseHdc( dc2 ); e.Graphics.DrawImage( memImage, 0, 0 ); } 3. Declare the API function. Please note the BitBlt function used in Step 2. It is an unmanaged function. You should use ‘DllImport’ attribute to import it to your code. Although, this is the Step 3, you may perform this step anytime. [C#] using System.Runtime.InteropServices; [ DllImport( ‘gdi32.dll’ ) ] private static extern bool BitBlt( IntPtr hdcDest, int nXDestint nYDest, int nWidthint nHeight, IntPtr hdcSrcint nXSrcint nYSrc, System.Int32 dwRop );

How can I Host the WPF UserControl in Windows Forms ?

This can be done with the help of the following code : [C#[ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Windows.Forms.Integration; namespace WpfUserControlHost { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Create the ElementHost control for hosting the // WPF UserControl. ElementHost host = new ElementHost(); host.Dock = DockStyle.Fill; // Create the WPF UserControl. HostingWpfUserControlInWf.UserControl1 uc = new HostingWpfUserControlInWf.UserControl1(); // Assign the WPF UserControl to the ElementHost control’s // Child property. host.Child = uc; // Add the ElementHost control to the form’s // collection of child controls. this.Controls.Add(host); } } }

What is Serialization ?

Serialization is the process of converting the state of the object to a stream, so that it can be saved and transported. The reverse process of serialization is called Deserialization, where a stream is converted back to an object. The objective of serialization process is persistence of objects in an application. There are two types of Serialization: ’Binary and ’SOAP’ serialization respectively.