WPF & Datargidview Control
The Windows Forms DataGridView control may be hosted in a WPF form using a <WindowsFormsHost> tag
(in fact any Windows Forms Control may be hosted within this tag, which provides a solution to the lack of a WPF Data Grid control).
The following XAML and VB.Net code was done using Visual Studio 2005 .
In a WPF Windows application:
- add a reference to WindowsFormsIntegration (WindowsFormsIntegration.dll)
In a XAML file (i.e. Window1.xaml)
- add a reference to System.Windows.Forms:
<Window
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ...
- add the following XAML to host the DataGridView control
<StackPanel>
<WindowsFormsHost Width="200" Height="200">
<wf:DataGridView x:Name="dgv"> </wf:DataGridView>
</WindowsFormsHost>
</StackPanel>
Note: it appears that the <WindowsFormsHost> should have Width and Height attributes to ensure that the DataGridView will render with scrollbars
In the associated code view for the XMAL(Windows.xaml.vb) :
- Add the following code to the Window1_Loaded event handler:
'Use the Authors table in the Pubs database
Dim conn As New System.Data.SqlClient.SqlConnection("integrated security=true;database=pubs")
Dim da As New System.Data.SqlClient.SqlDataAdapter("select * from employee", conn)
Dim ds As New System.Data.DataSetda.Fill(ds)
Me.dgv.DataSource = ds.Tables(0)
Me.dgv.DataMember = ds.Tables(0).TableName
Me.dgv.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.dgv.Visible = True
Run the program to display the WPF with the DataGridView.
Note that <WindowsFormsHost> may only contain one Windows Forms Control. Use multiple <WindowsFormsHost> tags or create a Windows Forms User Control with the desired layout and host in using in <WindowsFormsHost>. Also, the DataGridView will not render with any WPF styles that may have defined.
Flow Documents and Code Behind
It is possible to have XAML controls with event handlers in a Flow Document. This can be accomplished using Visual studio 2005 by:
- Add a FlowDocument (WPF) to a Visual Studio 2005 .NET 3.0 WPF project (i.e. FlowDocument2.xaml)
- Add a class to the project named FlowDocument2.xaml.vb (VS 2005 does not automatically add an associated "code-behind" class)
- In FlowDocument2.xaml in the <FlowDocument> element add the attribute: <FlowDocument a:Class="FlowDocument2" ....
- Within a <Paragraph> element add:
Test Text<InlineUIContainer><Button Name="TestCmd" Content="Press Me"></Button></InlineUIContainer>Test Text - Build the project
- In the Code-behind class (FlowDocument2.xaml.vb) for TestCmd (the button) add a click event
- In the click event place some code (i.e. MessageBox.Show("clicked")
- Make FlowDocument2.xaml the startup URI for the project
- Run the project (F5 or <Shift>F5
Labels: ted
XAML & Flow Documents
For WPF projects in Visual Studio 2005 you can have the project display a Flow Document when starting.
In the App.Xaml file, for the < Application> element add the following attribute:
StartupUri="FlowDocument1.xaml"
With this approach it is easy to see what the associated flow document looks like. Just run the project using F5 (Start Debugging) or <Shift> F5 (Start Without Debugging).
Note that Flow Documents cannot be viewed in design mode within Visual Studio 2005, so this technique provides an easy way to see the actual formatting of a Flow Document.
WPF and Windows Forms
While learning WPF, in order to take advantage of its neat features (i.e graphics, animation etc), you will most likely want to get busy using WPF in your projects as soon as possible. You can do this even if you are not up to speed on all WPF features.
It is possible to have both WPF Windows and traditional Windows Forms within the same VS 2005 .NET Framework 3.0 Windows Application (WPF). You can use WPF forms where you feel comfortable, but use Windows Forms for other situations.
For example, suppose that you have not yet got up to speed with WPF data binding -- you could implement that in a Win Form using the traditional data binding techniques, but do other application interfaces using WPF (i.e About Form, Search Form).
In .NET code, both WPF Windows and Windows Forms are objects and can be thought of as a "form"
For example:
Dim f as SomeForm 'could be a WPF or Win Form
f.show
You can show a WPF form from a Windows Form or show a Windows Form from a WPF form.
Some Beginning XAML
XMAL gives developers the means to customize their user interfaces in ways that have not been available using traditional tools (i.e. Windows Forms).
The following XMAL snippet gives a simple example:
<StackPanel Background="Beige">
<Button>
<Button.ToolTip>
<TextBlock FontWeight="UltraBold" TextAlignment="Center">
<Ellipse Width="20" Height="20" Stroke="Blue" Fill="AliceBlue"></Ellipse>
<Label>A ToolTip Message</Label>
<Rectangle Height="20" Width="20" Fill="AliceBlue" Stroke="Blue" StrokeThickness="2" ></Rectangle>
</TextBlock>
</Button.ToolTip>
<Button.Content>
<TextBlock FontWeight="UltraBold" TextAlignment="Center">
<Ellipse Width="20" Height="20" Stroke="Red" Fill="Pink"></Ellipse>
<Label>Press Me</Label>
<Rectangle Height="20" Width="20" Fill="Pink" Stroke="Red" StrokeThickness="2" ></Rectangle>
</TextBlock>
</Button.Content>
</Button>
</StackPanel>
The button defines its own content and a tooltip. Notice that the content and tool tip defintions can contain other XAML, allowing for very interesting presentation. Here, both the button and tool tip have tradtional text (i.e. Press Me) as well as a Rectangle and Ellipse. No longer are developers constrained by text only or creating their own images with hope that a control supports display of an image.
The other good news is that when the window with the above is resized, the button contents are automatically adjusted to ensure that it continues to have a consistent look and feel -- and the best part, a developer does not have to do anything to get this behavior!!
I'm Back
After a long, long break from blogging, I am back!! For a while I have been using/learning Windows Presentation Foundation. I find this feature of .NET 3.0 to have huge potential for changing, enhancing the way user interfaces are designed and implemented for applications.
I have been using VS 2005 with the Visual Studio 2005 extensions for .NET Framework 3.0. concentrating on standalone applications (the traditional Windows Forms application development that we all know and love)
The good news is that I am able to leverage my knowledge of Visual Studio in order to get right to work creating .xaml to define the UI along with implementation of business intelligence code (usually in VB.NET). Actually when beginning to learn/experiment with Windows Foundation Presentation, which more then likely means being exposed to XAML (eXtensible Application Markup Language) rather quickly, keep in mind what I like to think of as ".NET/Visual Studio" patterns.
For example in a VS 2005 NET Framework 3.0 Windows Application, when adding a new WPF Window to a project results in 2 files being added to that project, one for the xaml (aName.xmal) and one for managed code (i.e aName.xaml.vb). These files are associated with one another - in the xaml file the user interface is defined using XAML, while in the.xaml.vb file holds any associated managed code.
For ASP.NET Web developers this mirrors the "code-behind" approach; user interface defined in .aspx using HTML and code in the associated .aspx.vb file. As is the case with ASP.NET, you could decide to keep code (VB or C#) along with the XAML in the same .xaml file. This is analogous to an in-line ASP.NET approach.
My point here is to keep in mind familiar concepts and patterns about .NET and Visual Studio as they will appear quite often while learning WPF. Recognizing them will help speed up the learning curve.
ASP.NET 2.0 Gridview
I have been working with ASP.NET 2.0 on a variety of projects. When using the new Gridview control I have found that it does not support custom paging (as the datagrid did in ASP.NET 1.x). That's too bad, the first time I really want to use this facility and it is gone....
Visual Studio 2005 Missing Windows
As I was debugging with Visual Studio 2005 I wanted to display the Immediate window (old habits die hard). When I looked for it in as a Debug|Windows menu item it was not there. I searched around menu items some and could not find it.
Instead I used CTL+ALT+I to access (display) it.
The same issue occurred when I wanted to access (display) the Exceptions window. I had to use CTL+ALT+E to access (display) it.
I wonder if this was by Visual Studio 2005 design or not; in any case these 2 windows are available....