Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ChipGroup): Chip.Icon being overwritten #560

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)));
Xiaoy312 marked this conversation as resolved.
Show resolved Hide resolved

/// <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
Loading