views:

29

answers:

2

Hey all,

This should seem to be a fairly straightforward question, but I'm struggling a bit. I have a collection of objects that's I've bound to a Grid aling with a Series of GridViewColumns for each property of the object I'd like to display.

One of the columns, however, is currently displaying as an int and I'd like to convert it over to a string. If possible, I'd like the conversion to take place in a different class (at my business layer).

Here's what I have for that GridViewColumn in my XAML: <GridViewColumn Header="Status" DisplayMemberBinding="{Binding CourtEventStatusCodeId}" />

What does the method in my Business layer need to look like and how do I use it in conjuction with my XAML?

Thanks in advance,
Sonny

A: 

You need a ValueConverter - check out the Binding.Coverter property which should get you started on the right path.

You'd have something like this...

<UserControl.Resources>
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</UserControl.Resources>

...then when using...

Converter={StaticResource BoolToVisibilityConverter}

...and you can reuse wherever you like within the same project.

Will A
I've tried that, but all the code examples show it as part of the code behind for the specific page. Any way for me to locate this in a seperate class as I may need to call this from a number of different locations and I don't want to copy-and-paste.
Sonny Boy
You can easily create this in a separate class and reuse it on various XAMLs throughout your project.
Will A
How do I then access it from the XAML in my page? What would that code look like?
Sonny Boy
Edited to include sample code. Note that `BoolToVisibilityConverter` in my example is a class that implements `IValueConverter`.
Will A
@Will - I've added a class to the code behind that implments IValueConverter and also tried to add a local resource to my page. I'm getting an error in the XAML when I try to use this:`<local:CourtEventConverter x:Key="CourtEventConverter" />`The error is `The type 'local:CourtEventConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.`
Sonny Boy
You'll need to have added `xmlns:local="clr-namespace:namespace-of-your-DLL"` at the top of the XAML, where all the other xmlns reside.
Will A
@Will - Thank you, sir. All is good in the land of conversion.
Sonny Boy
A: 

Since the value is defined by the users and can't be an enum const, are the user defined values in the database? Maybe you want to make your SQL join the tables and return the column from the joined table with the string.

Jimmy Hoffa
This defeats the purpose of having a business or presentation layer. I don't want presentation logic in with my objects.
Sonny Boy
@Sonny Boy: Having a dal object represent an int column as an enum is no more presentation logic than representing an xml column as an xdocument. You wouldn't represent it as a string would you?
Jimmy Hoffa
@Jimmy - CourtEventStatusCode is a class which can represent any number of user-defined values. While I would prefer to have it defined as an enum which is determined at design-time, I'm afraid that isn't an option.
Sonny Boy
@Sonny Boy: Ah, you didn't specify it was design time defined, then yes a ValueConverter is what you want. Sorry for assuming it was an enum..
Jimmy Hoffa