开发者

Displaying a custom value type in WPF without using a Value Converter

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开发者_StackOverflow社区 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?


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜