Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix property grid search exception #740

Merged
merged 1 commit into from
Aug 18, 2017
Merged

Fix property grid search exception #740

merged 1 commit into from
Aug 18, 2017

Conversation

WilliamBZA
Copy link
Member

Connects to #733

  • Updates to the latest version of DevExpress that we are licensed for
  • Includes a value converter and behavior to wrap search strings in quotes - this prevents the exception from occurring

@WilliamBZA WilliamBZA requested a review from a team August 11, 2017 08:35
@adamralph adamralph requested review from adamralph and removed request for adamralph August 11, 2017 11:25
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("\"{0}\"", value);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be more for my own education than anything else, but looking purely at the method signatures, I would naively write these methods like so:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (targetType != typeof(string))
    {
        throw new InvalidOperationException($"Cannot convert value to target type '{targetType}'.");
    }

    if (value == null)
    {
        return null;
    }

    var stringValue = value as string;

    if (stringValue == null)
    {
        throw new InvalidOperationException($"The value is not a string.");
    }

    return stringValue.StartsWith("\"", true, culture) && stringValue.EndsWith("\"", true, culture)
        ? stringValue.Substring(1, stringValue.Length - 2)
        : stringValue;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
    string.Format(culture, "\"{0}\"", value);

However, I don't know what assumptions can be made wrt the WPF execution context, and what does or does not need to be done. Things to note about the above snippet:

  • Respecting the targetType argument and throwing if it's not a string
  • Short circuiting the case where value is null.
  • Avoiding the string allocation by safe casting value to a string.
  • Throwing if value is not a string
  • using the supplied culture argument where appropriate (in both methods)

Copy link
Member Author

@WilliamBZA WilliamBZA Aug 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a real fan of the exceptions. The converters are invoked by the databinding pipeline so any exceptions are treated as application exceptions (could crash?)

What about:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (targetType != typeof(string))
    {
        return Binding.DoNothing;
    }

    var stringValue = value as string;
    if (stringValue == null)
    {
        return DependencyProperty.UnsetValue;
    }

    return stringValue.StartsWith("\"", true, culture) && stringValue.EndsWith("\"", true, culture)
        ? stringValue.Substring(1, stringValue.Length - 2)
        : stringValue;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're both right! You could use exceptions in a converter and they show up as warnings, but the right approach is to use the DependencyProperty.UnsetValue as @WilliamBZA highlighted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WilliamBZA your suggestion LGTM.

@WilliamBZA
Copy link
Member Author

Updated based on feedback and rebased on master.

I think this is good to go now

using DevExpress.Xpf.Editors;
using ServiceInsight.ValueConverters;

public class ComboBoxEditValuePatcherBehavior : Behavior<ComboBoxEdit>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is the 'fix'? Should we put a comment there or somehow make it more obvious that this is not a generic behavior? Maybe a link to the bug report?

@adamralph adamralph self-requested a review August 18, 2017 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants