Skip to content

Commit

Permalink
fix(ChipGroup): Chip.Icon being overwritten (unoplatform#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 authored and roubachof committed May 16, 2023
1 parent 2c0acf2 commit ba40eba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Uno.Toolkit.UI/Controls/Chips/ChipGroup.Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ public bool CanRemove
nameof(IconTemplate),
typeof(DataTemplate),
typeof(ChipGroup),
new PropertyMetadata(null, (s, e) => (s as ChipGroup)?.ApplyIconTemplate()));
new PropertyMetadata(null, (s, e) => (s as ChipGroup)?.ApplyIconTemplate((DataTemplate?)e.OldValue, (DataTemplate?)e.NewValue)));

/// <summary>
/// Gets or sets the value of each <see cref="Chip.IconTemplate"/>.
/// </summary>
public DataTemplate IconTemplate
public DataTemplate? IconTemplate
{
get => (DataTemplate)GetValue(IconTemplateProperty);
get => (DataTemplate?)GetValue(IconTemplateProperty);
set => SetValue(IconTemplateProperty, value);
}

Expand Down
25 changes: 20 additions & 5 deletions src/Uno.Toolkit.UI/Controls/Chips/ChipGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void OnLoaded(object sender, RoutedEventArgs e)
_isLoaded = true;
SynchronizeInitialSelection();
EnforceSelectionMode();
ApplyIconTemplate();
ApplyIconTemplate(null, IconTemplate);
}

private void OnSelectionMemberPathChanged(DependencyPropertyChangedEventArgs e)
Expand All @@ -56,13 +56,28 @@ private void OnSelectionMemberPathChanged(DependencyPropertyChangedEventArgs e)
}
}

private void ApplyIconTemplate()
private void ApplyIconTemplate(DataTemplate? oldTemplate, DataTemplate? newTemplate)
{
var itemTemplate = IconTemplate;
if (oldTemplate == newTemplate) return;

foreach (var container in this.GetItemContainers<Chip>())
{
container.Icon = itemTemplate != null ? container.Content : null;
container.IconTemplate = itemTemplate;
if (newTemplate is not null)
{
// If there was no Icon assigned, use Content as Icon
// Otherwise the icon presenter will not display anything without an icon
container.Icon ??= container.Content;
container.IconTemplate = newTemplate;
}
else
{
// clear icon if we previously used content for the icon
if (container.Icon == container.Content)
{
container.Icon = null;
}
container.IconTemplate = null;
}
}
}

Expand Down

0 comments on commit ba40eba

Please sign in to comment.