views:

102

answers:

3

The system I'm working on uses a large number of custom value types internally. I'm working on a prototype UI using WPF. WPF does not know how to display the custom types. I know that I can write a custom ValueConverter to do this, but I really don't want to have to specify the use of a converter every time I bind to a class in my XAML.

Is there an Interface I can implement in the custom value type that will let WPF know how to display it natively?

Or, is there a way I can specify an application-wide value converter that will apply to any instance of our custom type, without having to specify the ValueConveter in every binding expression?

+1  A: 

You should be able to specify a DataTemplate for your value type, and put it in your application resources. This would determine how your Value type is displayed globally.

Reed Copsey
A: 

I'm sorry if this is so obvious it's stupid, but why don't you override ToString() in your value types?

Robert Rossney
I did. That doesn't work, at least for the case I'm dealing with, which is binding to a DataGrid.
Jason
A: 

Aha! Figured it out. I needed to write a TypeConverter and apply it to my custom types. This allows WPF to automatically figure out how to handle them without having to specify a template or converter in the XAML.

http://msdn.microsoft.com/en-us/library/ayybcxe5.aspx

Jason
**This is a bad idea, and a poor solution to the problem.** You should use a default `DataTemplate` in your `App.xaml` or `Themes/Generic.xaml` instead. Templating is what makes WPF powerful: The same object can look different in different parts of your application or in different application, and you get a clear separtion between your data layer and your UI. A fundamental principle of good WPF design is that your data layer should know nothing whatsoever about the UI. There are many good reasons for this. Using a TypeConverter that knows about the UI seriously violates this principle.
Ray Burns