Skip to content

Commit

Permalink
Re-render tab header or content when template changed
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed Jul 17, 2024
1 parent 0858000 commit 2252016
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/UraniumUI.Material/Controls/TabItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ public class TabItem : UraniumBindableObject

public DataTemplate HeaderTemplate { get => (DataTemplate)GetValue(HeaderTemplateProperty); set => SetValue(HeaderTemplateProperty, value); }

public static readonly BindableProperty HeaderTemplateProperty = BindableProperty.Create(nameof(HeaderTemplate), typeof(DataTemplate), typeof(TabItem), defaultBindingMode: BindingMode.TwoWay);
public static readonly BindableProperty HeaderTemplateProperty =
BindableProperty.Create(
nameof(HeaderTemplate),typeof(DataTemplate), typeof(TabItem),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) => (bindable as TabItem).TabView.RenderHeaders());

public DataTemplate ContentTemplate { get => (DataTemplate)GetValue(ContentTemplateProperty); set => SetValue(ContentTemplateProperty, value); }

public static readonly BindableProperty ContentTemplateProperty = BindableProperty.Create(nameof(ContentTemplate), typeof(DataTemplate), typeof(TabItem), defaultBindingMode: BindingMode.TwoWay);
public static readonly BindableProperty ContentTemplateProperty = BindableProperty.Create(
nameof(ContentTemplate), typeof(DataTemplate), typeof(TabItem),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: (bindable, oldValue, newValue) => (bindable as TabItem).TabView.Render());

public View Content { get; set; }
public View Header { get; set; }
Expand Down
30 changes: 25 additions & 5 deletions src/UraniumUI.Material/Controls/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,10 @@ private void Items_CollectionChanged(object sender, NotifyCollectionChangedEvent
}
}

protected virtual void Render()
internal virtual void Render()
{
if (Items?.Count > 0 || ItemsSource?.Count > 0)
{
_headerContainer.Children.Clear();
RenderHeaders();

if (SelectedTab is null)
Expand All @@ -305,14 +304,14 @@ protected virtual void Render()
}
}

protected virtual void RenderHeaders()
internal virtual void RenderHeaders()
{
_headerContainer.Children.Clear();
foreach (var item in Items)
{
AddHeaderFor(item);
}


if (ItemsSource is not null)
{
foreach (var item in ItemsSource)
Expand All @@ -322,9 +321,29 @@ protected virtual void RenderHeaders()
}
}

internal virtual void InvalidateTabItemContents()
{
foreach (var tabItem in this.Items)
{
tabItem.Content = null;
tabItem.Header = null;
}

ResetSelectedTab();
}

protected void ResetSelectedTab()
{
SelectedTab = Items.FirstOrDefault();
if (SelectedTab is null)
{
SelectedTab = Items.FirstOrDefault();
}
else
{
// Send previous selected tab to null, to force re-rendering.
// TODO: Create an API to force re-rendering for header, content or both.
OnSelectedTabChanged(null, SelectedTab).FireAndForget();
}
}

protected virtual void AddHeaderFor(TabItem tabItem)
Expand All @@ -333,6 +352,7 @@ protected virtual void AddHeaderFor(TabItem tabItem)
tabItem.Header =
tabItem.HeaderTemplate?.CreateContent() as View
?? TabHeaderItemTemplate?.CreateContent() as View
//?? DefaultTabHeaderItemTemplate.CreateContent() as View
?? throw new InvalidOperationException("TabView requires a HeaderTemplate or TabHeaderItemTemplate to be set.");

tabItem.Header.BindingContext = tabItem;
Expand Down

0 comments on commit 2252016

Please sign in to comment.