@@ -12,32 +12,23 @@ public class QuerySuggestionBoxConverter : IMultiValueConverter
1212 {
1313 public object Convert ( object [ ] values , Type targetType , object parameter , CultureInfo culture )
1414 {
15- if ( values . Length != 3 )
16- {
17- return string . Empty ;
18- }
19- var QueryTextBox = values [ 0 ] as TextBox ;
20-
21- var queryText = ( string ) values [ 2 ] ;
22-
23- if ( string . IsNullOrEmpty ( queryText ) )
24- return string . Empty ;
25-
26- // second prop is the current selected item result
27- var val = values [ 1 ] ;
28- if ( val == null )
29- {
15+ // values[0] is TextBox: The textbox displaying the autocomplete suggestion
16+ // values[1] is ResultViewModel: Currently selected item in the list
17+ // values[2] is string: Query text
18+ if (
19+ values . Length != 3 ||
20+ values [ 0 ] is not TextBox queryTextBox ||
21+ values [ 1 ] is null ||
22+ values [ 2 ] is not string queryText ||
23+ string . IsNullOrEmpty ( queryText )
24+ )
3025 return string . Empty ;
31- }
32- if ( ! ( val is ResultViewModel ) )
33- {
34- return System . Windows . Data . Binding . DoNothing ;
35- }
26+
27+ if ( values [ 1 ] is not ResultViewModel selectedItem )
28+ return Binding . DoNothing ;
3629
3730 try
3831 {
39- var selectedItem = ( ResultViewModel ) val ;
40-
4132 var selectedResult = selectedItem . Result ;
4233 var selectedResultActionKeyword = string . IsNullOrEmpty ( selectedResult . ActionKeywordAssigned ) ? "" : selectedResult . ActionKeywordAssigned + " " ;
4334 var selectedResultPossibleSuggestion = selectedResultActionKeyword + selectedResult . Title ;
@@ -50,17 +41,15 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
5041 // When user typed lower case and result title is uppercase, we still want to display suggestion
5142 selectedItem . QuerySuggestionText = queryText + selectedResultPossibleSuggestion . Substring ( queryText . Length ) ;
5243
53- // Check if Text will be larger then our QueryTextBox
54- System . Windows . Media . Typeface typeface = new Typeface ( QueryTextBox . FontFamily , QueryTextBox . FontStyle , QueryTextBox . FontWeight , QueryTextBox . FontStretch ) ;
44+ // Check if Text will be larger than our QueryTextBox
45+ Typeface typeface = new Typeface ( queryTextBox . FontFamily , queryTextBox . FontStyle , queryTextBox . FontWeight , queryTextBox . FontStretch ) ;
5546 // TODO: Obsolete warning?
56- System . Windows . Media . FormattedText ft = new FormattedText ( QueryTextBox . Text , System . Globalization . CultureInfo . DefaultThreadCurrentCulture , System . Windows . FlowDirection . LeftToRight , typeface , QueryTextBox . FontSize , Brushes . Black ) ;
47+ var ft = new FormattedText ( queryTextBox . Text , CultureInfo . DefaultThreadCurrentCulture , System . Windows . FlowDirection . LeftToRight , typeface , queryTextBox . FontSize , Brushes . Black ) ;
5748
58- var offset = QueryTextBox . Padding . Right ;
49+ var offset = queryTextBox . Padding . Right ;
5950
60- if ( ( ft . Width + offset ) > QueryTextBox . ActualWidth || QueryTextBox . HorizontalOffset != 0 )
61- {
51+ if ( ft . Width + offset > queryTextBox . ActualWidth || queryTextBox . HorizontalOffset != 0 )
6252 return string . Empty ;
63- } ;
6453
6554 return selectedItem . QuerySuggestionText ;
6655 }
0 commit comments