Dynamic UI with WPF and LINQ

Dynamic UI with WPF and LINQ

  • Comments 42

Lately I've been getting my hands deep into WPF with my line-of-business (LOB)/data-based application mind set. I'm taking a different approach to the technology resisting the urge to put on my amateur-designer hat and instead purely focus on data, data-binding, formatting controls, and some basic layout. (Yes before you ask, I have started producing the WPF forms over data videos!)

Today I wanted to play with dynamically creating XAML and loading it at runtime. This was really easy using XML literals and XML namespace imports. Let me show you what I mean. You can load and save XAML at runtime using the System.Windows.Markup.XamlReader and System.Windows.Markup.XamlWriter classes. Create a new WPF project and in the Window1 code-behind you can do this:

Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
Imports <xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Imports System.Windows.Markup

Class Window1

    Private Sub Window1_Loaded() Handles MyBase.Loaded

        Dim UI = <Label Name="Label1">This is COOL!</Label>

        Me.Content = XamlReader.Load(UI.CreateReader())

    End Sub
End Class

When we run it:

There are a lot of possibilities here. For instance, wouldn't it be nice to automatically generate all your maintenance screens in your business apps? When I say "maintenance" I mean all those simpler lookup data tables or contact tables. Even a customer contact form is usually pretty basic. Instead of handing those screens to the newbie or the junior on the team why not just generate them all at runtime based on your object model or database schema?

Admittedly this isn't something that is only unique to WPF. You could do this in Winforms as well but it was an exercise in coding the layout by hand. WPF makes this a breeze because we can define one piece of XAML and we can construct it from a single LINQ query. You can use reflection to look at your object model but if it's really simple and maps one-to-one to your database table anyway you can just create a table (or a stored proc) that contains the schema (or meta-data) of all of your maintenance tables. For instance to generate a simple UI we would probably want to obtain at minimum the following properties for each column in a table:

ColumnName
DataType
MaxLength
IsPrimaryKey

You can either create a meta-data table in your database or a stored proc that returns the info from the information schema (check permissions for the stored proc if you go that route). I was playing with the stored proc idea because then if I make a change to my database, I don't need to update my code necessarily. So for this example I created a stored proc in the Northwind database called GetTableSchema:

CREATE PROCEDURE dbo.GetTableSchema
    (
    @table varchar(50)
    )    
AS
SELECT 
c.table_name As TableName, 
c.column_name As ColumnName, 
c.data_type As DataType, 
c.character_maximum_length As MaxLength,
    COALESCE (
    ( SELECT 
        CASE cu.column_name
            WHEN null THEN 0
            ELSE 1
        END
    FROM information_schema.constraint_column_usage cu
    INNER join information_schema.table_constraints ct
    ON ct.constraint_name = cu.constraint_name
    WHERE 
    ct.constraint_type = 'PRIMARY KEY' 
    AND ct.table_name = c.table_name
    AND cu.column_name = c.column_name 
    ),0) AS IsPrimaryKey
FROM information_schema.columns c
INNER JOIN information_schema.tables t
ON c.table_name = t.table_name
WHERE @table = t.table_name and 
      (t.table_type = 'BASE TABLE' and not 
      (t.table_name = 'dtproperties') and not 
      (t.table_name = 'sysdiagrams'))
ORDER BY c.table_name, c.ordinal_position

I then added a new "LINQ to SQL classes" item to my project and dragged Customer onto the design surface (for this example that's what we'll be editing) from the Northwind database I had connected to in my Server Explorer. I then expanded the "Stored Procedures" and dragged the above procedure onto the methods pane. Next I manually created an object on the design surface called TableSchema that contained the same properties as the fields I'm returning from the stored proc. Once I have that all set up I can now map the result type of the stored proc to the TableSchema class:

Okay now that we have that set up we can get back to the fun stuff. I created a simple lookup textbox and "Find" and "Save" buttons at a fixed area at the top of my WPF window. Under that I dragged a ContentControl onto the form and named it DynamicContent. We're going to generate the content here from the Customer schema and bind to the customer object that is returned from our LINQ query when we click find.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1"  Name="Window1" SizeToContent="WidthAndHeight" >
    <Grid Name="MainGrid"  >
        <Grid.RowDefinitions>
            <RowDefinition Height="10*" />
            <RowDefinition Height="60*" />
        </Grid.RowDefinitions>
        <StackPanel Name="StackPanel1" Orientation="Horizontal" Margin="3" VerticalAlignment="Top">
            <Label Height="28" Name="Label1" Width="84" HorizontalContentAlignment="Right" FontWeight="Bold">ID</Label>
            <TextBox Height="25" Name="txtSearch" Width="120">ALFKI</TextBox>
            <Button Height="25" Name="btnFind" Width="75">Find</Button>
            <Button Height="25" Name="btnSave" Width="75">Save</Button>
        </StackPanel>
        <ContentControl Grid.Row="1" Name="DynamicContent" Margin="3" />
    </Grid>
</Window>

First let's generate the UI in the Load event of our form. The first thing is to add the appropriate Imports at the top of the file and then we can generate our UI. (I also have handlers here for the actual loading and saving of the customer.)

Imports <xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
Imports <xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
Imports System.Windows.Markup

Class Window1

    Dim db As New NorthwindDataContext
    Dim CustomerData As Customer

    Private Sub Window1_Loaded() Handles MyBase.Loaded

        Dim customerSchema = db.GetTableSchema("Customers").ToList()

        Dim UI = <Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="100*"/>
                         <ColumnDefinition Width="200*"/>
                     </Grid.ColumnDefinitions>
                     <StackPanel Name="StackLabels" Margin="3">
                         <%= From column In customerSchema _
                             Where column.IsPrimaryKey = 0 _
                             Select <Label
                                        Height="28"
                                        Name=<%= column.ColumnName & "Label" %>
                                        HorizontalContentAlignment="Right">
                                        <%= column.ColumnName %>:</Label> %>
                     </StackPanel>
                     <StackPanel Grid.Column="1" Name="StackFields" Margin="3">
                         <%= From column In customerSchema _
                             Where column.IsPrimaryKey = 0 _
                             Select GetUIElement(column) %>
                     </StackPanel>
                 </Grid>

        Me.DynamicContent.Content = XamlReader.Load(UI.CreateReader)

    End Sub

    Private Function GetUIElement(ByVal column As TableSchema) As XElement
        Select Case column.DataType
            Case "datetime", "int"
                Return <TextBox
                           Height="28"
                           Name=<%= "txt" & column.ColumnName %>
                           Text=<%= "{Binding Path=" & column.ColumnName & ", ValidatesOnDataErrors=True}" %>/>
            Case "bit"
                Return <CheckBox
                           HorizontalContentAlignment="Left"
                           Name=<%= "chk" & column.ColumnName %>
                           IsChecked=<%= "{Binding Path=" & column.ColumnName & ", ValidatesOnDataErrors=True}" %>>
                           <%= column.ColumnName %>
                       </CheckBox>
            Case Else
                Return <TextBox
                           Height="28"
                           Name=<%= "txt" & column.ColumnName %>
                           MaxLength=<%= column.MaxLength %>
                           Text=<%= "{Binding Path=" & column.ColumnName & ", ValidatesOnDataErrors=True}" %>/>
        End Select
    End Function

    Private Sub btnFind_Click() Handles btnFind.Click
        If Me.txtSearch.Text <> "" Then
            Me.CustomerData = (From cust In db.Customers _
                              Where cust.CustomerID = Me.txtSearch.Text).FirstOrDefault()

            Me.DataContext = Me.CustomerData
        Else
            Me.DataContext = Nothing
        End If
    End Sub

    Private Sub btnSave_Click() Handles btnSave.Click
        If Me.DataContext IsNot Nothing Then
            Try
                db.SubmitChanges()
                MsgBox("Saved")

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub
End Class

The stored proc will work for any table contained in the database, so we could even abstract this further, and I obviously didn't get very fancy with the UI -- but I think you get the idea.

UPDATE: Read the latest post on dynamic data entry and download the code samples.

Enjoy!

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • Thanks Beth, I'll study it over the weekend.  

    Working on another video.  I really want to tip my hat to you, you make creating those videos look easy, but it really is a LOT of work.

    Thanks,

    John.

  • Beth,

    In your example on building ui from schema table I have copied as is and get this error when calling the XamlReader.Load

    XML namespace prefix does not map to a namespace URI, so cannot resolve Height.

    As is mind you.

    i am doing this in c# if that is any help.

    Please help

    Malcolm

  • Hi Malcom,

    I believe you'll need to control the XML namespaces manually in C#. VB does this automatically with the XML namespace imports at the top of the file.

    Take a look at: http://msdn.microsoft.com/en-us/library/bb387069.aspx

    HTH,

    -B

  • In case anyone else runs into this,

    I had to change information_schema to upper case in the stored proc because my db was set to case sensitive.

  • Excellent post! Any thoughts on converting this to a web app?

  • Hi Beth

    Excellent Article.What to I have to do if I want to try it out the same in C#

  • Hi Beth,

    Excellent post, this helps me to develop a dynamioc WPF appilication in VB.Net

    I am trying to do the same in C#, could you please help me in this regard?

    Thanks in Advance

    Krishna Prasad

    rvkrishnaprasad@gmail.com

  • hi Beth,

    your example on building ui from schema table is very good. I have tried to develop the same in c#, but unfortunately i didn't.

    Please help me by providing a sample c# code to build wpf ui from schema.

    your help is highly appreciated.

    Thanks.

    Lakshmi Narayana Vejendla

    narayana.vejendla@hotmail.com

  • Hi Beth,

    Excellent post, this helps me to develop a dynamic WPF appilication in VB.Net

    I am trying to do the same in C#, could you please help me in this regard?

    Thanks in Advance

    Krishna Prasad

    rvskrishnaprasad@gmail.com

    krishnaprasad_rvs@rediffmail.com

  • Thanks Beth for the excellent article. I've included a link below to a C# port of your primary example (just the .cs file and only the dynamic display bits, not the find/save methods).

    I'm teaching an internal class on WPF and XAML and wanted an example of how XAML can be used dynamically. However, since we only develop in C#, I needed to port it...I didn't realize how differently VB handles XML until I started the port.

    The first attempt just used StringBuilder to manually build the XML. Once I figured out the syntactic differences, I had no problems what-so-ever.

    However, I then decided to utilize the new XDocument/XElement classes. I immediately ran into the namespace issues mentioned by others.

    After dozens of Google searches and numerous tests, trials and tribulations, I found that when you have a default namespace, you simply need to prepend all elements with the namespace, and you're set.

    A second, still unresolved issue occurred when I attempted to load the XML with the XDocument.CreateReader() method. I ran into a namespace error on the Grid.Column attribute. The work-around was to first load it into a text reader. A bit of searching didn't turn up much (or rather turned up way to much that wasn't on target), so I'll leave this for a rainy day, or someone else with the interest in debugging it.

    Note that I didn't attempt to make a real-world application out of this...the sole purpose is as a training exercise, so it was left in the code-behind with a hard-coded call to a single table.

    You'll need to follow the article to create the initial page, stored procedure and connecting bits (and add the find/save methods)...as mentioned at the top, I've only included the C# for dynamic display of the XAML.

    I hadn't used LINQ to SQL yet (I use one of the commercial O/R mapper's that support LINQ for production code), and Beth's step-by-step example worked perfectly the first time.

    MSDN Code Gallery location:

    http://code.msdn.microsoft.com/dynamicWpfCSharp

  • Hi Beth,

    I finally got VS2008 :) about time eh lol

    Unfortunately, I wasn't able to get the whole suite, just the express editions, which is OK for now seeing as it gives me opportunity to experiment.

    I was just going through this article and I noticed that, once I'd added a connection to the Northwind database that came with Visual Basic 2008, my Database Explorer didn't show me a Stored Procedures item, so I just wanted to ask if you could tell me where I can write my procs.

    Thanks a lot!

    -L

  • How to call a custom control using  XamlReader.Load, Any Idea?

Page 3 of 3 (42 items) 123