Skip to content

Commit d410c05

Browse files
Mitigate NullReferenceException in AutoSuggestBox (#3546)
* Use nameof() in AutoSuggestBox and remove some whitespaces * Mitigate NullReferenceException
1 parent 7da7746 commit d410c05

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/MaterialDesignThemes.Wpf/AutoSuggestBox.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public class AutoSuggestBox : TextBox
1414

1515
#region Dependency Properties
1616

17-
public IEnumerable Suggestions
17+
public IEnumerable? Suggestions
1818
{
1919
get => (IEnumerable)GetValue(SuggestionsProperty);
2020
set => SetValue(SuggestionsProperty, value);
2121
}
2222

2323
public static readonly DependencyProperty SuggestionsProperty =
24-
DependencyProperty.Register("Suggestions", typeof(IEnumerable), typeof(AutoSuggestBox), new PropertyMetadata(null));
24+
DependencyProperty.Register(nameof(Suggestions), typeof(IEnumerable), typeof(AutoSuggestBox), new PropertyMetadata(null));
2525

2626

2727
public string ValueMember
@@ -30,7 +30,7 @@ public string ValueMember
3030
set => SetValue(ValueMemberProperty, value);
3131
}
3232
public static readonly DependencyProperty ValueMemberProperty =
33-
DependencyProperty.Register("ValueMember", typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
33+
DependencyProperty.Register(nameof(ValueMember), typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
3434

3535

3636
public string DisplayMember
@@ -39,47 +39,47 @@ public string DisplayMember
3939
set => SetValue(DisplayMemberProperty, value);
4040
}
4141
public static readonly DependencyProperty DisplayMemberProperty =
42-
DependencyProperty.Register("DisplayMember", typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
42+
DependencyProperty.Register(nameof(DisplayMember), typeof(string), typeof(AutoSuggestBox), new PropertyMetadata(default(string)));
4343

4444
public Brush DropDownBackground
4545
{
4646
get => (Brush)GetValue(DropDownBackgroundProperty);
4747
set => SetValue(DropDownBackgroundProperty, value);
4848
}
4949
public static readonly DependencyProperty DropDownBackgroundProperty =
50-
DependencyProperty.Register("DropDownBackground", typeof(Brush), typeof(AutoSuggestBox), new PropertyMetadata(default(Brush)));
50+
DependencyProperty.Register(nameof(DropDownBackground), typeof(Brush), typeof(AutoSuggestBox), new PropertyMetadata(default(Brush)));
5151

5252
public DataTemplate ItemTemplate
5353
{
5454
get => (DataTemplate)GetValue(ItemTemplateProperty);
5555
set => SetValue(ItemTemplateProperty, value);
5656
}
5757
public static readonly DependencyProperty ItemTemplateProperty =
58-
DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(AutoSuggestBox), new PropertyMetadata(default(DataTemplate)));
58+
DependencyProperty.Register(nameof(ItemTemplate), typeof(DataTemplate), typeof(AutoSuggestBox), new PropertyMetadata(default(DataTemplate)));
5959

6060
public Style ItemContainerStyle
6161
{
6262
get => (Style)GetValue(ItemContainerStyleProperty);
6363
set => SetValue(ItemContainerStyleProperty, value);
6464
}
6565
public static readonly DependencyProperty ItemContainerStyleProperty =
66-
DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(AutoSuggestBox), new PropertyMetadata(default(Style)));
66+
DependencyProperty.Register(nameof(ItemContainerStyle), typeof(Style), typeof(AutoSuggestBox), new PropertyMetadata(default(Style)));
6767

6868
public Elevation DropDownElevation
6969
{
7070
get => (Elevation)GetValue(DropDownElevationProperty);
7171
set => SetValue(DropDownElevationProperty, value);
7272
}
7373
public static readonly DependencyProperty DropDownElevationProperty =
74-
DependencyProperty.Register("DropDownElevation", typeof(Elevation), typeof(AutoSuggestBox), new PropertyMetadata(default(Elevation)));
74+
DependencyProperty.Register(nameof(DropDownElevation), typeof(Elevation), typeof(AutoSuggestBox), new PropertyMetadata(default(Elevation)));
7575

7676
public double DropDownMaxHeight
7777
{
7878
get => (double)GetValue(DropDownMaxHeightProperty);
7979
set => SetValue(DropDownMaxHeightProperty, value);
8080
}
8181
public static readonly DependencyProperty DropDownMaxHeightProperty =
82-
DependencyProperty.Register("DropDownMaxHeight", typeof(double), typeof(AutoSuggestBox), new PropertyMetadata(200.0));
82+
DependencyProperty.Register(nameof(DropDownMaxHeight), typeof(double), typeof(AutoSuggestBox), new PropertyMetadata(200.0));
8383

8484

8585
public bool IsSuggestionOpen
@@ -88,15 +88,15 @@ public bool IsSuggestionOpen
8888
set => SetValue(IsSuggestionOpenProperty, value);
8989
}
9090
public static readonly DependencyProperty IsSuggestionOpenProperty =
91-
DependencyProperty.Register("IsSuggestionOpen", typeof(bool), typeof(AutoSuggestBox), new PropertyMetadata(default(bool)));
91+
DependencyProperty.Register(nameof(IsSuggestionOpen), typeof(bool), typeof(AutoSuggestBox), new PropertyMetadata(default(bool)));
9292

9393
public object SelectedItem
9494
{
9595
get => GetValue(SelectedItemProperty);
9696
set => SetValue(SelectedItemProperty, value);
9797
}
9898
public static readonly DependencyProperty SelectedItemProperty =
99-
DependencyProperty.Register("SelectedItem", typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
99+
DependencyProperty.Register(nameof(SelectedItem), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
100100

101101

102102
public object SelectedValue
@@ -105,15 +105,11 @@ public object SelectedValue
105105
set => SetValue(SelectedValueProperty, value);
106106
}
107107
public static readonly DependencyProperty SelectedValueProperty =
108-
DependencyProperty.Register("SelectedValue", typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
109-
110-
111-
112-
108+
DependencyProperty.Register(nameof(SelectedValue), typeof(object), typeof(AutoSuggestBox), new PropertyMetadata(default(object)));
113109

114110
public static readonly RoutedEvent SuggestionChosenEvent =
115111
EventManager.RegisterRoutedEvent(
116-
"SuggestionChosen",
112+
nameof(SuggestionChosen),
117113
RoutingStrategy.Bubble,
118114
typeof(RoutedPropertyChangedEventHandler<object>),
119115
typeof(AutoSuggestBox));
@@ -253,7 +249,7 @@ private void CommitValueSelection(object? selectedValue)
253249

254250
private void DecrementSelection()
255251
{
256-
if (_autoSuggestBoxList is null)
252+
if (_autoSuggestBoxList is null || Suggestions is null)
257253
return;
258254
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
259255
if (collectionView.IsCurrentBeforeFirst)
@@ -265,7 +261,7 @@ private void DecrementSelection()
265261

266262
private void IncrementSelection()
267263
{
268-
if (_autoSuggestBoxList is null)
264+
if (_autoSuggestBoxList is null || Suggestions is null)
269265
return;
270266
ICollectionView collectionView = CollectionViewSource.GetDefaultView(Suggestions);
271267
if (collectionView.IsCurrentAfterLast)

0 commit comments

Comments
 (0)