Thursday, July 26, 2007

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

  • Build the Project

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.

Sunday, July 08, 2007

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:

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.