Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell ViewModel in Template10

I'm using Template 10 with a hamburger menu for my project. To change the buttons dynamically depending on wether the user is logged in or not, I figured I'd create a ViewModel for the Shell. Unfortunately, it would seem the DataContext is not correctly targeted to the ViewModel.

I've added the data context to the Shell.xaml, but bindings aren't doing their thing. Is there a better way of going about this, or am I doing something wrong.

Shell.xaml snippets:

Setting the data context:

<Page.DataContext>
        <viewModels:ShellViewModel x:Name="ViewModel" />
</Page.DataContext>

Binding visibility to viewmodel:

<Controls:HamburgerButtonInfo AutomationProperties.Name="My Journey"
                                      ClearHistory="False"
                                      PageType="views:MyJourneyPage"
                                      Visibility="{Binding LoggedIn, Converter={StaticResource BooleanToVisibilityConverter}}">

ShellViewModel snippet:

private bool _loggedIn;
public bool LoggedIn { get { return _loggedIn; } set { Set(ref _loggedIn, value); } }

Visual Studio is reporting: Error: BindingExpression path error: 'LoggedIn' property not found on 'Template10.Controls.HamburgerButtonInfo'. BindingExpression: Path='LoggedIn' DataItem='Template10.Controls.HamburgerButtonInfo'; target element is 'Template10.Controls.HamburgerButtonInfo' (Name='null'); target property is 'Visibility' (type 'Visibility')

like image 392
LostLogic Avatar asked May 01 '26 19:05

LostLogic


2 Answers

Resolved by changing the visibility binding to:

 "{x:Bind Path=ViewModel.LoggedIn, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverterInverse}}"
like image 126
LostLogic Avatar answered May 03 '26 09:05

LostLogic


It doesn't see the path for the viewmodel and that property you created for the login, Path = LoggedIn, DataContext= ViewModel depending on the depth you might have to find the combination for the DataContext. This is more than likely due to the DataContext that the DataItem sees right now, related to the collection it is part of in the hamburger control.

like image 30
mvermef Avatar answered May 03 '26 07:05

mvermef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!