Skip to content

Commit

Permalink
Fix issue with collection change inside item invoked crashing Navigat…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Aug 19, 2020
1 parent 02f76b4 commit 08209c5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
8 changes: 7 additions & 1 deletion ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,13 @@ void RaiseItemInvokedForNavigationViewItem(NavigationViewItem nvi)
{
var inspectingDataSource = (InspectingDataSource)itemsSourceView;
var itemIndex = parentIR.GetElementIndex(nvi);
nextItem = inspectingDataSource.GetAt(itemIndex);

// Check that index is NOT -1, meaning it is actually realized
if (itemIndex != -1)
{
// Something went wrong, item might not be realized yet.
nextItem = inspectingDataSource.GetAt(itemIndex);
}
}

// Determine the recommeded transition direction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
<StackPanel Orientation="Horizontal">
<muxcontrols:NavigationView
x:Name="NavView"
AutomationProperties.Name="NavView"
Expand All @@ -28,6 +28,19 @@
</StackPanel>
</StackPanel>
</muxcontrols:NavigationView>
</Grid>

<muxcontrols:NavigationView
Width="400" PaneDisplayMode="Left"
MenuItemsSource="{x:Bind Pages, Mode=OneWay}"
ItemInvoked="NavigationView_ItemInvoked">
<muxcontrols:NavigationView.MenuItemTemplate>
<DataTemplate>
<muxcontrols:NavigationViewItem Content="{Binding Name}" AutomationProperties.Name="{Binding Name}"/>
</DataTemplate>
</muxcontrols:NavigationView.MenuItemTemplate>

<TextBlock Text="{Binding SelectedPage.Name}" />
</muxcontrols:NavigationView>
</StackPanel>

</local:TestPage>
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,33 @@
using NavigationView = ModernWpf.Controls.NavigationView;
using NavigationViewSelectionChangedEventArgs = ModernWpf.Controls.NavigationViewSelectionChangedEventArgs;
using NavigationViewItem = ModernWpf.Controls.NavigationViewItem;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Collections.Generic;

namespace MUXControlsTestApp
{
public sealed partial class NavigationViewPageDataContext : TestPage
public sealed partial class NavigationViewPageDataContext : TestPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private NavViewPageData[] _pages;

public NavViewPageData[] Pages
{
get => _pages;
set
{
_pages = value;
NotifyPropertyChanged(nameof(Pages));
}
}

public NavigationViewPageDataContext()
{
this.InitializeComponent();
Expand All @@ -32,6 +54,32 @@ public NavigationViewPageDataContext()
}

NavView.SelectedItem = NavView.MenuItems[0];

Pages = new[]
{
new NavViewPageData("FirstInvokeChangeItem"),
new NavViewPageData("SecondInvokeChangeItem"),
new NavViewPageData("ThirdInvokeChangeItem"),
new NavViewPageData("FourthInvokeChangeItem"),
new NavViewPageData("FifthInvokeChangeItem"),
new NavViewPageData("SixthInvokeChangeItem"),
new NavViewPageData("SeventhInvokeChangeItem"),
};
}

private void NavigationView_ItemInvoked(NavigationView sender, ModernWpf.Controls.NavigationViewItemInvokedEventArgs args)
{
if (_pages != null && _pages.Length == 0)
{
return;
}

var page = Pages[0];
var newPages = new List<NavViewPageData>();
newPages.Add(new NavViewPageData($"Clicked {page.Name}"));
newPages.AddRange(Pages);

Pages = newPages.ToArray();
}

private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
Expand All @@ -45,5 +93,15 @@ private void GetNavViewActiveVisualStates_Click(object sender, RoutedEventArgs e
var visualstates = Utilities.VisualStateHelper.GetCurrentVisualStateName(NavView);
NavViewActiveVisualStatesResult.Text = string.Join(",", visualstates);
}

public class NavViewPageData
{
public string Name { get; set; }

public NavViewPageData(string name)
{
Name = name;
}
}
}
}

0 comments on commit 08209c5

Please sign in to comment.