Archive for the ‘Databinding’ category

Draggable WPF Controls

March 19th, 2012

I have a project where I needed to have WPF controls placed on a canvas and those controls have the ability to be dragged or positioned around on the screen somewhere.  I was using some custom logic I had written to draw rectangles and textboxes to the canvas and just watching the mouse events but that was a hack because sometimes the text would get too big to fit in the textboxes.  I came across this blog post and Josh Smith has a nice little DragCanvas class that wraps up all this functionality very nicely, all you have to do is put the controls in the Xaml and you are set, very nice job Josh!

http://www.codeproject.com/Articles/15354/Dragging-Elements-in-a-Canvas

This is nice for me because I wanted to display data in a grid and have headers and be able to drag those grids around the screen.  It also allows you to do other things like set transparencies on those controls and hide and show them as well.

How to bind an enumeration (enum) to a WPF/Xaml ComboBox

February 16th, 2012

You may already know this, but if you don’t I hope it is of some help to somebody out there looking to do this in their code. If you have an enum or enumeration and you simply want to have it as a choice in your UI and also want to bind it to a property on your View Model, I have attached the code to do this below, check it out.

public enum Enum {
        none, 
        Choice1, 
        Choice2
    }

<UserControl.Resources>
        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="vm:Enum" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </UserControl.Resources>

<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" IsSynchronizedWithCurrentItem="true" SelectedItem="{Binding PropertyToBindTo, Mode=TwoWay}" Width="100"/>

I am using a UserControl in my example but a standard Xaml window would work as well. Notice also that the key is named as so (x:Key=”odp”) you use that down in your combobox as a static resource since we are placing this in the resources of the user control. If you make a property with the enum as its type you can bind right to the SelectedItem just make sure that you set it to Mode=TwoWay or it may not work. The (ObjectType=”{x:Type sys:Enum}”) simply tells wpf that you are presenting it with an enum type that uses the ‘sys’ namespace. In This line ‘‘ the ‘vm’ is simply the namespace that your Enum sits. I hope this can help somebody out there wanting to implement this type of thing.

GrapeCity ActiveReports (Formerly DataDynamics ActiveReports) DataBinding

February 1st, 2012

I have been a long time user of Active Reports for Visual Basic 6 and then in the .net world, I think I have used every version of ActiveReports since they released the first version.  I wanted to write about a little known fact that you may or may not know about this tool that could save you from writing tons of logic to get the data displayed you want.  Lets say you have a couple of simple classes like below:

public class Person{
     public string Name {get;set;}
     public Job Job {get; set;}
}

public class Job {
     public string Status {get; set;}
}

You may already know that just by simply making any field on your report with the DataField of “Name” will automatically bind that property to that field as long as your datasource is set to a collection of Person objects. But did you know that the DataField can work like so:

So as you can see, if your Person object has a Job object in it and there is a string property on there called status, it will display it as long as the property is public. Another caveat to this is if you have a null object in that string, you may not get an error tossed up to your UI, which could make for some fun debugging. Even still, this is a very nice feature that I have long forgotten but recently came across and it was one of those forehead slapping times.