how to programatically open and close bottomappbar using a eventhandler that exist in a usercontrol back code?

拈花ヽ惹草 提交于 2019-12-13 01:47:57

问题


I have a Windows 8 application (Xaml and C#):

I have a userControl that is included in a MainPage.

In this MainPage i have included a BottomAppBar that is fully functioning on RightClick and Windows+Z.

What i need to do is to open the BottomAppBar from an event handler (in the usercontrol back code) on an image that exists in the userControl. i need to access the BottomAppBar in order to use the property IsOpen, but i am not being able to do so. Any hint? am i missing anything?


回答1:


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));
        }
    }
}


来源:https://stackoverflow.com/questions/19162445/how-to-programatically-open-and-close-bottomappbar-using-a-eventhandler-that-exi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!