Skip to content

Commit

Permalink
Correct implementation for readonly DependcyProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
timunie committed Feb 27, 2021
1 parent 82fb357 commit 443746f
Showing 1 changed file with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,40 +138,47 @@ private static bool IsValidSelectionMode(object o)
|| value == SelectionMode.Extended;
}

/// <summary>Identifies the <see cref="SelectedItems"/> dependency property.</summary>
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(

internal static readonly DependencyPropertyKey SelectedItemsPropertyKey =
DependencyProperty.RegisterReadOnly(
nameof(SelectedItems),
typeof(IList),
typeof(MultiSelectionComboBox),
new PropertyMetadata((IList)null));

/// <summary>Identifies the <see cref="SelectedItems"/> dependency property.</summary>
public static readonly DependencyProperty SelectedItemsProperty = SelectedItemsPropertyKey.DependencyProperty;


/// <summary>
/// The currently selected items.
/// </summary>
[Bindable(true), Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public IList SelectedItems
{
get
{
return PART_PopupListBox?.SelectedItems;
}
get { return (IList)GetValue(SelectedItemsProperty); }
protected set { SetValue(SelectedItemsPropertyKey, value); }
}

/// <summary>Identifies the <see cref="DisplaySelectedItems"/> dependency property.</summary>
public static readonly DependencyProperty DisplaySelectedItemsProperty =
DependencyProperty.Register(


internal static readonly DependencyPropertyKey DisplaySelectedItemsPropertyKey =
DependencyProperty.RegisterReadOnly(
nameof(DisplaySelectedItems),
typeof(IEnumerable),
typeof(MultiSelectionComboBox),
new PropertyMetadata((IEnumerable)null));

/// <summary>Identifies the <see cref="DisplaySelectedItems"/> dependency property.</summary>
public static readonly DependencyProperty DisplaySelectedItemsProperty = DisplaySelectedItemsPropertyKey.DependencyProperty;

/// <summary>
/// Gets the <see cref="SelectedItems"/> in the specified order which was set via <see cref="OrderSelectedItemsBy"/>
/// </summary>
public IEnumerable DisplaySelectedItems
{
get { return (IEnumerable)GetValue(DisplaySelectedItemsProperty); }
protected set { SetValue(DisplaySelectedItemsPropertyKey, value); }
}

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

/// <summary>Identifies the <see cref="HasCustomText"/> dependency property.</summary>
public static readonly DependencyProperty HasCustomTextProperty =
DependencyProperty.Register(

internal static readonly DependencyPropertyKey HasCustomTextPropertyKey =
DependencyProperty.RegisterReadOnly(
nameof(HasCustomText),
typeof(bool),
typeof(MultiSelectionComboBox),
new PropertyMetadata(false));


/// <summary>Identifies the <see cref="HasCustomText"/> dependency property.</summary>
public static readonly DependencyProperty HasCustomTextProperty = HasCustomTextPropertyKey.DependencyProperty;

/// <summary>
/// Indicates if the text is userdefined
/// </summary>
public bool HasCustomText
{
get { return (bool)GetValue(HasCustomTextProperty); }
protected set { SetValue(HasCustomTextPropertyKey, BooleanBoxes.Box(value)); }
}

/// <summary>Identifies the <see cref="TextWrapping"/> dependency property.</summary>
Expand Down Expand Up @@ -351,7 +363,7 @@ public IParseStringToObject StringToObjectParser
/// </summary>
public void ResetEditableText()
{
SetCurrentValue(HasCustomTextProperty, BooleanBoxes.FalseBox);
HasCustomText = false;
UpdateEditableText();
}

Expand Down Expand Up @@ -580,18 +592,18 @@ private void UpdateHasCustomText(string selectedItemsText)

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

SetCurrentValue(HasCustomTextProperty, BooleanBoxes.Box(hasCustomText));
HasCustomText = hasCustomText;
}

private void UpdateDisplaySelectedItems(OrderSelectedItemsBy orderBy)
{
if (orderBy == OrderSelectedItemsBy.SelectedOrder)
{
SetCurrentValue(DisplaySelectedItemsProperty, SelectedItems);
DisplaySelectedItems = SelectedItems;
}
else if (orderBy == OrderSelectedItemsBy.ItemsSourceOrder)
{
SetCurrentValue(DisplaySelectedItemsProperty, ((IEnumerable<object>)PART_PopupListBox.SelectedItems).OrderBy(o => Items.IndexOf(o)));
DisplaySelectedItems= ((IEnumerable<object>)SelectedItems).OrderBy(o => Items.IndexOf(o));
}
}

Expand Down Expand Up @@ -860,6 +872,7 @@ public override void OnApplyTemplate()
{
PART_PopupListBox.SelectionChanged -= PART_PopupListBox_SelectionChanged;
PART_PopupListBox.SelectionChanged += PART_PopupListBox_SelectionChanged;
SelectedItems = PART_PopupListBox.SelectedItems;
}
else
{
Expand Down

0 comments on commit 443746f

Please sign in to comment.