|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWith WPF, Microsoft introduced a new XML based descriptive language for use when designing interfaces called XAML. To help you use the new language, the company built in the VS forms designer support for XAML. Also, the Microsoft Expression Interactive Designer can be used to create such files. But how about code-based layout? How did it change? Charles Petzold seems to think that it was left out from WPF layout improvements and designing code-only interfaces has now become harder than ever. Let's see if he is right. Possible uses of this non-XAML approach in WPF are:
PrerequisitesTo understand the code, you should have a basic understanding of XAML and WPF layout using XAML. If you have experience dynamically creating .NET 2.0 forms using code, it's even better. Using the codeThe code was written in VS 2005 with the June CTP of .NET 3.0 installed, it might not work with another CTP. It contains an example of code-only WPF window layout in a .NET 2.0 class library project, being used in a .NET 2.0 EXE application. Changes in WPF layout from .NET 2.0First of all, WPF made standard the resolution-independent way of designing Windows. In .NET 2.0 you had to manually position each control on the screen using co-ordinates and you could, with a certain amount of additional work, use resolution independent panels. In WPF, panels are the standard way of doing things so you should understand them well. A Form can hold only one child object, in its General steps in creating a WPF window from codeWe will use a normal .NET 2.0 class project to create our WPF window in. Before we start, we should make sure we have all the prerequisites listed. Then, as we are not going to use a .NET 3.0 type of project, we must add references to the used assemblies. A reference is created by right-clicking on the References directory in your Solution Explorer. Then select Add Reference, then .NET. Add these DLLs: PresentationCore, PresentationFramework, and WindowsBase. They contain the objects we need from .NET 3.0. Now we can start coding in a new class file added to your project (Right click on your project in Solution Explorer, Add-> New->Class).
Layout example with StackPanelThe public class StackPanelWindow:System.Windows.Window
{
StackPanel stack;
Button btn;
TextBox text;
public StackPanelWindow()
{
this.Width = 300;
this.Height = 150;
//add the StackPanel
stack = new StackPanel();
stack.Width = this.Width;
stack.Height = this.Height;
this.Content = stack;
//add the text field
text = new TextBox();
text.Width = 100;
text.Height = 20;
text.Text = "some text";
stack.Children.Add(text);
//add the button
btn = new Button();
btn.IsEnabled = true;
btn.Width = 100;
btn.Height = 20;
btn.Content = "Say it!";
stack.Children.Add(btn);
}
We see that step 7 is missing; the controls are automatically positioned one under the previous one. Layout example with DockPanelThe DockPanel.SetDock(text, Dock.Top);
DockPanel.SetDock(btn, Dock.Bottom);
If we add a fifth control and so there is no side to dock it, it can be fitted in the center. Layout example with GridThe //create two columns
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
//and two rows
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
Then to position each control in the Grid.SetColumn(text, 0);
Grid.SetRow(text, 0);
Layout example with CanvasThe canvas was the standard .NET 2.0 way of doing things. It allows you to position each control on a canvas with precise co-ordinates, like this: //inherit from the WPF Window object
public class CanvasWindow:System.Windows.Window
{
private Canvas canvas;
private TextBox text;
private Button btn;
public CanvasWindow()
{
//first, set the window properties
this.Width = 300;
this.Height = 150;
//add the canvas
canvas=new Canvas();
canvas.Width = this.Width;
canvas.Height = this.Height;
this.Content =canvas;
//add a text field
text = new TextBox();
text.Width = 100;
text.Height = 20;
text.Margin = new System.Windows.Thickness(10,10,10,10);
text.Text = "some text";
text.KeyDown += new System.Windows.Input.KeyEventHandler(text_KeyDown);
canvas.Children.Add(text);
//add a button
btn = new Button();
btn.IsEnabled = true;
btn.Width = 100;
btn.Height = 20;
btn.Content = "Say it!";
btn.Margin = new System.Windows.Thickness(110, 10, 10, 10);
btn.Click += new System.Windows.RoutedEventHandler(btn_Click);
canvas.Children.Add(btn);
}
The positioning was made by this line: Control.Margin = new System.Windows.Thickness(10,10,10,10);
which is a general way to make sure your object is where it should be. If you want to use another approach, particular to the Canvas.SetLeft( control_in_canvas , value );
and Canvas.SetTop( control_in_canvas , value );
Note that layout created using the More Complex DesignFor mode complex design, the panels should be used one in another. An example is: use a
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||