Skip to content

Commit 443746f

Browse files
committed
Correct implementation for readonly DependcyProperties
1 parent 82fb357 commit 443746f

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/MahApps.Metro/Controls/MultiSelectionComboBox/MultiSelectionComboBox.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,40 +138,47 @@ private static bool IsValidSelectionMode(object o)
138138
|| value == SelectionMode.Extended;
139139
}
140140

141-
/// <summary>Identifies the <see cref="SelectedItems"/> dependency property.</summary>
142-
public static readonly DependencyProperty SelectedItemsProperty =
143-
DependencyProperty.Register(
141+
142+
internal static readonly DependencyPropertyKey SelectedItemsPropertyKey =
143+
DependencyProperty.RegisterReadOnly(
144144
nameof(SelectedItems),
145145
typeof(IList),
146146
typeof(MultiSelectionComboBox),
147147
new PropertyMetadata((IList)null));
148148

149+
/// <summary>Identifies the <see cref="SelectedItems"/> dependency property.</summary>
150+
public static readonly DependencyProperty SelectedItemsProperty = SelectedItemsPropertyKey.DependencyProperty;
151+
152+
149153
/// <summary>
150154
/// The currently selected items.
151155
/// </summary>
152156
[Bindable(true), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
153157
public IList SelectedItems
154158
{
155-
get
156-
{
157-
return PART_PopupListBox?.SelectedItems;
158-
}
159+
get { return (IList)GetValue(SelectedItemsProperty); }
160+
protected set { SetValue(SelectedItemsPropertyKey, value); }
159161
}
160162

161-
/// <summary>Identifies the <see cref="DisplaySelectedItems"/> dependency property.</summary>
162-
public static readonly DependencyProperty DisplaySelectedItemsProperty =
163-
DependencyProperty.Register(
163+
164+
165+
internal static readonly DependencyPropertyKey DisplaySelectedItemsPropertyKey =
166+
DependencyProperty.RegisterReadOnly(
164167
nameof(DisplaySelectedItems),
165168
typeof(IEnumerable),
166169
typeof(MultiSelectionComboBox),
167170
new PropertyMetadata((IEnumerable)null));
168171

172+
/// <summary>Identifies the <see cref="DisplaySelectedItems"/> dependency property.</summary>
173+
public static readonly DependencyProperty DisplaySelectedItemsProperty = DisplaySelectedItemsPropertyKey.DependencyProperty;
174+
169175
/// <summary>
170176
/// Gets the <see cref="SelectedItems"/> in the specified order which was set via <see cref="OrderSelectedItemsBy"/>
171177
/// </summary>
172178
public IEnumerable DisplaySelectedItems
173179
{
174180
get { return (IEnumerable)GetValue(DisplaySelectedItemsProperty); }
181+
protected set { SetValue(DisplaySelectedItemsPropertyKey, value); }
175182
}
176183

177184
/// <summary>Identifies the <see cref="OrderSelectedItemsBy"/> dependency property.</summary>
@@ -246,20 +253,25 @@ public string Separator
246253
set { SetValue(SeparatorProperty, value); }
247254
}
248255

249-
/// <summary>Identifies the <see cref="HasCustomText"/> dependency property.</summary>
250-
public static readonly DependencyProperty HasCustomTextProperty =
251-
DependencyProperty.Register(
256+
257+
internal static readonly DependencyPropertyKey HasCustomTextPropertyKey =
258+
DependencyProperty.RegisterReadOnly(
252259
nameof(HasCustomText),
253260
typeof(bool),
254261
typeof(MultiSelectionComboBox),
255262
new PropertyMetadata(false));
256263

264+
265+
/// <summary>Identifies the <see cref="HasCustomText"/> dependency property.</summary>
266+
public static readonly DependencyProperty HasCustomTextProperty = HasCustomTextPropertyKey.DependencyProperty;
267+
257268
/// <summary>
258269
/// Indicates if the text is userdefined
259270
/// </summary>
260271
public bool HasCustomText
261272
{
262273
get { return (bool)GetValue(HasCustomTextProperty); }
274+
protected set { SetValue(HasCustomTextPropertyKey, BooleanBoxes.Box(value)); }
263275
}
264276

265277
/// <summary>Identifies the <see cref="TextWrapping"/> dependency property.</summary>
@@ -351,7 +363,7 @@ public IParseStringToObject StringToObjectParser
351363
/// </summary>
352364
public void ResetEditableText()
353365
{
354-
SetCurrentValue(HasCustomTextProperty, BooleanBoxes.FalseBox);
366+
HasCustomText = false;
355367
UpdateEditableText();
356368
}
357369

@@ -580,18 +592,18 @@ private void UpdateHasCustomText(string selectedItemsText)
580592

581593
bool hasCustomText = !((string.IsNullOrEmpty(selectedItemsText) && string.IsNullOrEmpty(Text)) || string.Equals(Text, selectedItemsText, EditableTextStringComparision));
582594

583-
SetCurrentValue(HasCustomTextProperty, BooleanBoxes.Box(hasCustomText));
595+
HasCustomText = hasCustomText;
584596
}
585597

586598
private void UpdateDisplaySelectedItems(OrderSelectedItemsBy orderBy)
587599
{
588600
if (orderBy == OrderSelectedItemsBy.SelectedOrder)
589601
{
590-
SetCurrentValue(DisplaySelectedItemsProperty, SelectedItems);
602+
DisplaySelectedItems = SelectedItems;
591603
}
592604
else if (orderBy == OrderSelectedItemsBy.ItemsSourceOrder)
593605
{
594-
SetCurrentValue(DisplaySelectedItemsProperty, ((IEnumerable<object>)PART_PopupListBox.SelectedItems).OrderBy(o => Items.IndexOf(o)));
606+
DisplaySelectedItems= ((IEnumerable<object>)SelectedItems).OrderBy(o => Items.IndexOf(o));
595607
}
596608
}
597609

@@ -860,6 +872,7 @@ public override void OnApplyTemplate()
860872
{
861873
PART_PopupListBox.SelectionChanged -= PART_PopupListBox_SelectionChanged;
862874
PART_PopupListBox.SelectionChanged += PART_PopupListBox_SelectionChanged;
875+
SelectedItems = PART_PopupListBox.SelectedItems;
863876
}
864877
else
865878
{

0 commit comments

Comments
 (0)