Skip to content

Commit d2514a3

Browse files
committed
Simplify ConvertTo functionality
1 parent a13bb63 commit d2514a3

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,29 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
114114
{
115115
ArgumentNullException.ThrowIfNull(destinationType);
116116

117-
if (destinationType == typeof(string) && value != null)
117+
if (destinationType == typeof(string) && value is not null)
118118
{
119-
MouseAction mouseActionValue = (MouseAction)value ;
120-
if (MouseActionConverter.IsDefinedMouseAction(mouseActionValue))
119+
return (MouseAction)value switch
121120
{
122-
string mouseAction = null;
123-
switch (mouseActionValue)
124-
{
125-
case MouseAction.None : mouseAction=String.Empty; break;
126-
case MouseAction.LeftClick : mouseAction="LeftClick"; break;
127-
case MouseAction.RightClick : mouseAction="RightClick"; break;
128-
case MouseAction.MiddleClick : mouseAction="MiddleClick"; break;
129-
case MouseAction.WheelClick : mouseAction="WheelClick"; break;
130-
case MouseAction.LeftDoubleClick : mouseAction="LeftDoubleClick"; break;
131-
case MouseAction.RightDoubleClick : mouseAction="RightDoubleClick"; break;
132-
case MouseAction.MiddleDoubleClick: mouseAction="MiddleDoubleClick"; break;
133-
}
134-
if (mouseAction != null)
135-
return mouseAction;
136-
}
137-
throw new InvalidEnumArgumentException("value", (int)mouseActionValue, typeof(MouseAction));
121+
MouseAction.None => string.Empty,
122+
MouseAction.LeftClick => "LeftClick",
123+
MouseAction.RightClick => "RightClick",
124+
MouseAction.MiddleClick => "MiddleClick",
125+
MouseAction.WheelClick => "WheelClick",
126+
MouseAction.LeftDoubleClick => "LeftDoubleClick",
127+
MouseAction.RightDoubleClick => "RightDoubleClick",
128+
MouseAction.MiddleDoubleClick => "MiddleDoubleClick",
129+
_ => throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(MouseAction))
130+
};
138131
}
132+
139133
throw GetConvertToException(value,destinationType);
140134
}
141135

142-
// Helper like Enum.IsDefined, for MouseAction.
136+
// Helper like Enum.IsDefined, for MouseAction.
143137
internal static bool IsDefinedMouseAction(MouseAction mouseAction)
144138
{
145-
return (mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick);
139+
return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick;
146140
}
147141
}
148142
}

0 commit comments

Comments
 (0)