http://www.codeproject.com/KB/WPF/GuidedTourWPF_3.aspx
There are a couple of things to notice about this Window’s XAML. Its DataContext is assigned an ObjectDataProvider object
that calls the static GetEmployees method of my Employee class. Once the DataContext is set to that array of Employee
objects, setting the ItemsControl’s ItemsSource to “{Binding}” means that the control will display all of those Employees.
We don’t need a loop in the code-behind to create EmployeeControl instances for each Employee. This is because the
ItemsControl’s ItemTemplate property is set to a DataTemplate that will create an EmployeeControl for each Employee in the
list. Since the ItemsControl’s ItemsSource is bound to an array of Employee objects and it’s ItemTemplate knows how to
create an EmployeeControl, there is no reason to write any code like we saw in the WinForms example.
I had to edit the XAML file by hand to create the XML namespace aliases, the ObjectDataProvider, the ItemsControl’s
ItemTemplate, and even the ItemsControl itself. I’m not sure why the Visual Studio Toolbox does not contain ItemsControl by
default. I also had to edit the XAML to include all bindings, since Cider does not allow you to create data bindings on the
design surface.
The code-behind for EmployeeControl is also empty, except for the obligatory call to InitializeComponent that is automatically
written when you create a new UserControl. Unlike the WinForms app, I was able to create all of the data bindings without
writing code (but I did have to create them all in XAML).
For the purpose of this article, we will be looking at the following:
Canvas
StackPanel
WrapPanel
DockPanel
Grid
DockPanel
The DockPanel control is one of the most useful (IMHO) layout controls. It is the one that we would probably use as the base
layout control that any new Window uses. Basically with a DockPanel control (or 2), we can achieve the sort of layout that
has been the main layout for most applications we have ever seen. We can basically get a menu docked to the top, then a
left/right main content area, and a status strip at the bottom. This is all thanks to a couple of properties on the DockPanel
control. Basically we can control the docking of any of our child controls that is within a parent DockPanel control by the use
of the following dependency/attached property.
DockPanel.Dock
This property may be set to Left/Right/Top or Bottom. There is one further property exposed as a normal CLR property on the
DockPanel control which is called LastChildFill which when set to true will make the last child control that was added to the
DockPanel control, fill the remaining available space. This will override any DockPanel.Dock property that the child control
may have already set.
What’s the difference between ComboBox’s IsEditable and IsReadOnly
properties?
Setting IsEditable to true turns ComboBox’s selection box into a text box. IsReadOnly
controls whether that text box can be edited, just like TextBox’s IsReadOnly property. This
means that IsReadOnly is meaningless unless IsEditable is true, and IsEditable being
true doesn’t necessarily mean that the selection text can be edited. Table 4.1 sums up the
behavior of ComboBox based on the values of these two properties.
TABLE 4.1 The Behavior for All Combinations of IsEditable and IsReadOnly