-
Notifications
You must be signed in to change notification settings - Fork 711
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
Hide NavigationViewItem Chevron if Children is empty #2759
Changes from 4 commits
1663ac7
7906e46
50577c7
98ff096
a56fa72
4750dce
69fc1bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -759,5 +759,70 @@ public void VerifyNavigationViewItemInFooterDoesNotCrash() | |
Verify.IsTrue(true); | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void VerifyExpandCollapseChevronVisibility() | ||
{ | ||
NavigationView navView = null; | ||
NavigationViewItem parentItem = null; | ||
ObservableCollection<string> children = null; | ||
|
||
RunOnUIThread.Execute(() => | ||
{ | ||
navView = new NavigationView(); | ||
Content = navView; | ||
|
||
children = new ObservableCollection<string>(); | ||
parentItem = new NavigationViewItem() { Content = "ParentItem", MenuItemsSource = children }; | ||
|
||
navView.MenuItems.Add(parentItem); | ||
|
||
navView.Width = 1008; // forces the control into Expanded mode so that the menu renders | ||
Content.UpdateLayout(); | ||
|
||
UIElement chevronUIElement = (UIElement)FindVisualChildByName(parentItem, "ExpandCollapseChevron"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also removed the FindVisualChildByName method. |
||
Verify.IsTrue(chevronUIElement.Visibility == Visibility.Collapsed, "chevron should have been collapsed as NavViewItem has no children"); | ||
|
||
// Add a child to our NavigationView parentItem. This should make the chevron visible. | ||
children.Add("Undo"); | ||
Content.UpdateLayout(); | ||
|
||
Verify.IsTrue(chevronUIElement.Visibility == Visibility.Visible, "chevron should have been visible as NavViewItem now has children"); | ||
|
||
// Remove all children from our NavigationView parentItem. This should collapse the chevron | ||
children.Clear(); | ||
Content.UpdateLayout(); | ||
|
||
Verify.IsTrue(chevronUIElement.Visibility == Visibility.Collapsed, "chevron should have been collapsed as NavViewItem no longer has children"); | ||
}); | ||
} | ||
|
||
private static DependencyObject FindVisualChildByName(FrameworkElement parent, string name) | ||
{ | ||
if (parent.Name == name) | ||
{ | ||
return parent; | ||
} | ||
|
||
int childrenCount = VisualTreeHelper.GetChildrenCount(parent); | ||
|
||
for (int i = 0; i < childrenCount; i++) | ||
{ | ||
FrameworkElement childAsFE = VisualTreeHelper.GetChild(parent, i) as FrameworkElement; | ||
|
||
if (childAsFE != null) | ||
{ | ||
DependencyObject result = FindVisualChildByName(childAsFE, name); | ||
|
||
if (result != null) | ||
{ | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now we check if the chevron is collapsed when the bound data collection is empty. We should probably also check in this test if the chevron visibility is updated accordingly when the NavigationViewItem.MenuItemsSource is set to
null
. (I.e. you could add an item again to the bound collection after our final visibility check after clearing the collection, then setparentItem.MenuItemsSource = null;
and verify again if the chevron is collapsed.)Right now we only use the
NavigationViewItem.MenuItemsSource
API to verify the correctness of the chevron visibility. For completeness, we could also add testing if the chevron visibility is updated correctly when using theNavigationViewItem.MenuItems
API instead. Testing with this API would simply add/remove items to/from MenuItems (as MenuItems cannot be set tonull
) and check the chevron visibility accordingly. These checks could be added inside this method below our tests for theMenuItemsSource
API (for example after clearing the bound collection or setting MenuItemsSource to null) so that we start testing using the MenuItems API with the chevron in collapsed state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there no other tests that would verify the other branches of the
HasChildren
function? I thought that some other tests verified this implicitly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chingucoding I haven't immediately found any other test (API/Interaction test) which we could fall back to here.
Edit: Just noticed there is no public
NavigationViewItem.HasChildren
API so ignore what I previously wrote here about having to create a test for it too 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that are no tests that test collection changes in the MenuItems API. Would be best if we add those here or if we create a tracking issue to do that later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am adding the tests for the MenuItems API now. Will push it once it passes the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All tests have passed even the tests for the MenuItems API.