wpfui icon indicating copy to clipboard operation
wpfui copied to clipboard

NavigationView - MenuItemsSource Binding issue when used within a UserControl

Open Coloris opened this issue 2 years ago • 5 comments
trafficstars

Describe the bug

When a NavigationView control is set inside a UserControl View, the MenuItemsSource doesn't reflect the binding on the UI

image

image

Other properties are correctly set from the same ViewModel (TitleBar Title and IsPaneToggleVisible from the NavigationView control)

The MenuItemsSource property binding functions properly when used directly in the MainWindow as a View.

To Reproduce

See https://github.com/Coloris/wpfsandbox/tree/main

Expected behavior

The Binding should work and the UI display the MenuItems

Screenshots

No response

OS version

Windows 11 22h2 - Version 22621.1848 Visual Studio 2022 - Version 17.6.4

.NET version

7.0

WPF-UI NuGet version

3.0.0-preview.3

Additional context

No response

Coloris avatar Jul 07 '23 15:07 Coloris

Also, when I add PresentationTraceSources.TraceLevel=High to the MenuItemsSource property binding, I get the following :

BindingExpression (hash=30400195): GetValue at level 0 from NavigationViewControlViewModel (hash=39567352) using RuntimePropertyInfo(NavigationItems): ObservableCollection`1 (hash=17472816 Count=2)

Created BindingExpression (hash=30400195) for Binding (hash=35943562) BindingExpression:Path=NavigationItems; DataItem=null;

Path: 'NavigationItems'
BindingExpression (hash=30400195): Default mode resolved to OneWay
BindingExpression (hash=30400195): Default update trigger resolved to PropertyChanged
BindingExpression (hash=30400195): Attach to Wpf.Ui.Controls.Navigation.NavigationView.MenuItemsSource (hash=64479624)
BindingExpression (hash=30400195): Resolving source
BindingExpression (hash=30400195): Found data context element: NavigationView (hash=64479624) (OK)
BindingExpression (hash=30400195): DataContext is null
BindingExpression (hash=30400195): Resolve source deferred
BindingExpression (hash=30400195): Resolving source
BindingExpression (hash=30400195): Found data context element: NavigationView (hash=64479624) (OK)
BindingExpression (hash=30400195): DataContext is null
BindingExpression (hash=30400195): Resolving source
BindingExpression (hash=30400195): Found data context element: NavigationView (hash=64479624) (OK)
BindingExpression (hash=30400195): Activate with root item NavigationViewControlViewModel (hash=39567352)
BindingExpression (hash=30400195):   At level 0 - for NavigationViewControlViewModel.NavigationItems found accessor RuntimePropertyInfo(NavigationItems)
BindingExpression (hash=30400195): Replace item at level 0 with NavigationViewControlViewModel (hash=39567352), using accessor RuntimePropertyInfo(NavigationItems)
BindingExpression (hash=30400195): TransferValue - got raw value ObservableCollection`1 (hash=17472816 Count=2)
BindingExpression (hash=30400195): TransferValue - using final value ObservableCollection`1 (hash=17472816 Count=2)

Coloris avatar Jul 07 '23 17:07 Coloris

` this._MenuItems = new ObservableCollection() { new NavigationViewItem() { Content="游戏", Icon = new SymbolIcon { Symbol = SymbolRegular.CheckboxChecked24 }, Foreground=Brushes.Black, Background=Brushes.White, MenuItems = new ObservableCollection() { new NavigationViewItem() { Content="魔兽争霸", Foreground=Brushes.Black, Icon = new SymbolIcon { Symbol = SymbolRegular.CheckboxChecked24 }, } } } };

        this.RootNavigation.MenuItems = _MenuItems;`

What is the difference between MenuItems and MenuItemsSource?I also encountered a problem with the navigation menu not being displayed

image

image

image

BoyFaceGirl avatar Jul 24 '23 06:07 BoyFaceGirl

Has any solution been found?

yooud avatar Nov 15 '23 00:11 yooud

Tried with the latest 3.0.0-preview.8 and the issue is still present.

Coloris avatar Nov 15 '23 00:11 Coloris

i also have problems with NavigationViewItem inside NavigationViewItem in XAML and binding in code, but it works for me

<ui:NavigationView
   Grid.Row="1"
   IsBackButtonVisible="Collapsed"
   IsPaneToggleVisible="True"
   Margin="5,0"
   OpenPaneLength="310"
   PaneDisplayMode="Left"
   x:Name="RootNavigation">
   <ui:NavigationView.MenuItems>
        <ui:NavigationViewItem Content="Tables" TargetPageType="{x:Type pages:FlightPage}" />
        <ui:NavigationViewItem Content="Seating" TargetPageType="{x:Type pages:SeatingPage}" />
    </ui:NavigationView.MenuItems>
</ui:NavigationView>
public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        (RootNavigation.MenuItems[0] as NavigationViewItem)!.MenuItems = new object[]
        {
            new NavigationViewItem("Flight", typeof(FlightPage)),
            new NavigationViewItem("Person", typeof(PersonPage)),
            new NavigationViewItem("Booking", typeof(BookingPage))
        };
    }
}

doesn't look good

yooud avatar Nov 15 '23 01:11 yooud