What is best practise for IValueConverter?

前端 未结 3 782
萌比男神i
萌比男神i 2021-01-02 17:14
  • What is best practise for IValueConverter?
  • Is it ok to put Exception in Convert method or should it return \"something\"?
相关标签:
3条回答
  • 2021-01-02 18:02

    If you fail to convert (malformed values, types, ...), return DependencyProperty.UnsetValue.

    It indicates that the converter produced no value and that the binding uses the FallbackValue, if available, or the default value instead.

    Also, you should convert data with culture-specific conversion or invariant conversions to be on the safe side.

    0 讨论(0)
  • 2021-01-02 18:05

    I personally recommend using singleton converters. Then you don't have to create an instance at every usage site, but can reference the converter like this:

    Converter={x:Static SomeNamespace:SomeConverter.Instance}
    
    0 讨论(0)
  • 2021-01-02 18:09

    You've ignored CultureInfo while parsing the string.

    Always take in to account the culture info passed otherwise it would always work on Thread's CurrentCulture.

    I could give some thing like "7.34,123" as input, would your code work?

    0 讨论(0)
提交回复
热议问题