开发者

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

开发者 https://www.devze.com 2022-12-18 11:03 出处:网络
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 cus

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

精彩评论

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