w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
how to programatically open and close bottomappbar using a eventhandler that exist in a usercontrol back code?

I am giving you simplest example, which may guide you how to do it.

Normal Way

BlankPage4.xaml

<Page.BottomAppBar>
    <AppBar IsSticky="True" IsOpen="True">
        <Button Style="{StaticResource BackButtonStyle}" />
    </AppBar>
</Page.BottomAppBar>

<Grid Background="{StaticResource
ApplicationPageBackgroundThemeBrush}">
    <local:MyUserControl1 />
</Grid>

MyUserControl1.xaml

<Grid>
    <Button Click="btnClose_CLick" Content="Close AppBar" />
</Grid>

MyUserControl1.xaml.cs

private void btnClose_CLick(object sender, RoutedEventArgs e)
{
    var isOpen =
((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen;
    if (isOpen)
    {
       
((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen =
false;
    }
    else
    {
       
((AppBar)((BlankPage4)((Grid)this.Parent).Parent).BottomAppBar).IsOpen =
true;
    }
}

MVVM Way

BlankPage4.xaml

<Page.BottomAppBar>
    <AppBar IsSticky="True" IsOpen="{Binding IsOpenBottomBar}">
        <Button Style="{StaticResource BackButtonStyle}" />
    </AppBar>
</Page.BottomAppBar>

<Grid Background="{StaticResource
ApplicationPageBackgroundThemeBrush}">
    <local:MyUserControl1 />
</Grid>

MyUserControl1.xaml.cs

private void btnClose_CLick(object sender, RoutedEventArgs e)
{
    var isOpen = (this.DataContext as ViewModel).IsOpenBottomBar;
    if (isOpen)
    {
        (this.DataContext as ViewModel).IsOpenBottomBar = false;
    }
    else
    {
        (this.DataContext as ViewModel).IsOpenBottomBar = true;
    }
}

ViewModel.cs

public class ViewModel : INotifyPropertyChanged
{
    private bool _IsOpenBottomBar;
    public bool IsOpenBottomBar
    {
        get
        {
            return _IsOpenBottomBar;
        }
        set
        {
            _IsOpenBottomBar = value;
            OnPropertyChanged("IsOpenBottomBar");
        }
    }

    public ViewModel()
    {
        _IsOpenBottomBar = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.