-
Notifications
You must be signed in to change notification settings - Fork 32
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
Conversation
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
return string.Format("\"{0}\"", value); | ||
} |
There was a problem hiding this comment.
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 astring
- Short circuiting the case where
value
isnull
. - Avoiding the string allocation by safe casting
value
to astring
. - Throwing if value is not a
string
- using the supplied
culture
argument where appropriate (in both methods)
There was a problem hiding this comment.
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;
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WilliamBZA your suggestion LGTM.
24f8161
to
567520a
Compare
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> |
There was a problem hiding this comment.
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?
147a858
to
849eea4
Compare
849eea4
to
9da7974
Compare
Connects to #733