Skip to content

Commit

Permalink
Merge pull request #541 from enisn/tabview-events
Browse files Browse the repository at this point in the history
Add events to TabView
  • Loading branch information
enisn authored Jan 19, 2024
2 parents 7164805 + 657ac82 commit 8aba635
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/UraniumUI.Material/Controls/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public partial class TabView : Grid

public bool UseAnimation { get; set; } = true;

public event EventHandler<object> CurrentItemChanged;
public event EventHandler<TabItem> SelectedTabChanged;

public static DataTemplate DefaultTabHeaderItemTemplate => new DataTemplate(() =>
{
var grid = new Grid();
Expand Down Expand Up @@ -392,10 +395,12 @@ protected virtual void OnCurrentItemChanged(object newItem)
return;
}

CurrentItemChanged?.Invoke(this, newItem);

SelectedTab = Items.FirstOrDefault(x => x.Data == newItem);
}

protected virtual async void OnSelectedTabChanged(TabItem oldValue, TabItem newValue)
protected virtual async Task OnSelectedTabChanged(TabItem oldValue, TabItem newValue)
{
if (newValue == null)
{
Expand Down Expand Up @@ -466,10 +471,14 @@ protected virtual async void OnSelectedTabChanged(TabItem oldValue, TabItem newV
}
}

content.Opacity = 1;

if (SelectedTab?.Data != null)
{
CurrentItem = SelectedTab.Data;
}

SelectedTabChanged?.Invoke(this, newValue);
}

protected virtual void OnTabPlacementChanged()
Expand Down Expand Up @@ -532,4 +541,4 @@ protected internal void NotifyIsSelectedChanged()
{
OnPropertyChanged(nameof(IsSelected));
}
}
}

4 comments on commit 8aba635

@malsabi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If also possible to support Commands for MVVM, such as CurrentItemChangedCommand, SelectedTabChangedCommand.

@enisn
Copy link
Owner Author

@enisn enisn commented on 8aba635 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but in the ViewModel, SelectedItem setter is already triggered, and the change can be handled in the setter, or if you use Community Toolkit MVVM or ReactiveUI, you can easily handle the changes of a property.

But still, I'll consider to add commands, too. Thanks 🙏

@malsabi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for considering my suggestion 🙏 about supporting Commands for MVVM like CurrentItemChangedCommand and SelectedTabChangedCommand. I appreciate your point about handling changes directly in the ViewModel's SelectedItem setter.

While these methods are indeed effective for property change management, incorporating commands can offer additional benefits. Commands can provide a more explicit and declarative way of handling user interactions, making the intent clearer and the code more maintainable. They can also simplify the logic in the ViewModel, especially for more complex scenarios or when actions need to be triggered that go beyond just setting a property.

@enisn
Copy link
Owner Author

@enisn enisn commented on 8aba635 Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.