简体   繁体   中英

How to bind a visibility of HamburgerButtonInfo to another viewmodel other than ShellViewModel?

Question

  1. How can I change the visibility of my hamburger button by not using shellviewmodel? Ps : Its an UWP app working on template10.
  2. Is it possible to bind the Hamburger button visibility to two different Viewmodels in template10?

Shell.xaml

xmlns:vm="using:ScanWorx.ViewModels"
xmlns:converters="using:ScanWorx.ViewModels"

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

<Page.Resources>
    <converters:BooleanToVisibilityConverter x:Key="Converter" />
</Page.Resources>

<controls:HamburgerMenu x:Name="MyHamburgerMenu">
    <controls:HamburgerMenu.PrimaryButtons>
        <!--  HeatMap Generator Button  -->
        <controls:HamburgerButtonInfo  ClearHistory="True" PageType="views:HeatMapGeneratorPage" 
                                       Visibility="{x:Bind vm:LoginPageViewModel.abc, Mode=TwoWay, 
                                                    Converter={StaticResource Converter}}">
            <StackPanel Orientation="Horizontal">
                <SymbolIcon
                    Width="48"
                    Height="48"
                    Symbol="ViewAll" />
                <TextBlock
                    Margin="12,0,0,0"
                    VerticalAlignment="Center"
                    Text="HeatMap Generator" />
            </StackPanel>
        </controls:HamburgerButtonInfo>

    </controls:HamburgerMenu.PrimaryButtons>
</controls:HamburgerMenu>

LoginPageViewModel.cs

namespace ScanWorx.ViewModels
{
   class LoginPageViewModel : ViewModelBase
   {
      public static bool abc { get; set; }
      public static Visibility visibility1 { get; set; }

      public async void Button_Login_Click()
      {
         try
         {
            *code to decrypt my login details and then after decryptng i check for next condition*
            if (ApplicationData.Current.LocalSettings.Values["UserAccessLevel"].ToString() == "Administrator")
            {
                abc = true;
                visibility1 = Visibility.Visible;
            }
            else
            {
                abc = false;
                visibility1 = Visibility.Collapsed;
            }
         }
      }
   }
}

ShellViewModel.cs :

  • If i use shellviewModel in binding The hiding works fine by changing - Visibility="{x:Bind vm:ShellViewModel.ShowButton} , but with this it can only be hard coded either false or true making it visible or collapsed once when app starts.
  • But i dont want to change the visibility of my button with shell view model i want to change it after i log in with the LoginPageViewModel*

     namespace ScanWorx.ViewModels { class ShellViewModel: ViewModelBase { public static bool ShowButton { get; set; } public ShellViewModel() { ShowButton = false; } } }

How to bind a visibility of HamburgerButtonInfo to another viewmodel other than ShellViewModel?

The better way is make a global setting class to record if login. After login success, edit the setting class ShowButton bool property to true.

Setting.cs

public class Setting : INotifyPropertyChanged
{
    private bool _showBtn = false;
    public bool ShowBtn
    {
        get { return _showBtn; }
        set { _showBtn = value; OnPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

Xaml bind

<Application.Resources>
    <ResourceDictionary>
        <local:Setting x:Key="Setting"/>
    </ResourceDictionary>
</Application.Resources>

......

Visibility="{Binding ShowBtn, Source={StaticResource Setting}}"

Value Change

((Setting)Application.Current.Resources["Setting"]).ShowBtn = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM