lepoco / wpfui

WPF UI provides the Fluent experience in your known and loved WPF framework. Intuitive design, themes, navigation and new immersive controls. All natively and effortlessly.
https://wpfui.lepo.co
MIT License
7.64k stars 744 forks source link

NavigationView - MenuItemsSource Binding issue when used within a UserControl #700

Open Coloris opened 1 year ago

Coloris commented 1 year ago

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 commented 1 year ago

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)
BoyFaceGirl commented 1 year ago

` 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

yooud commented 11 months ago

Has any solution been found?

Coloris commented 11 months ago

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

yooud commented 11 months ago

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