WPF - 页面导航

时间:2015-02-02 17:51:52

标签: wpf navigation

我正在VS 2012中创建一个WPF应用程序。 我的MainWindow充当导航到的页面的页眉/页脚包装器。 在我的页脚中,我添加了自定义后退/前进按钮,但我希​​望它们的可见性根据CanGoBack和CanGoForward属性进行更改。

我可以创建隐藏/显示按钮的C#例程,但该函数仅在初始化时运行一次。我需要这些函数在每次加载新页面时触发。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

是一个简单的例子,我使用Grids作为Pages,我使用Grid数组来保存网格的实例。我希望这会对您的情况有所帮助。

XAML:

<Window x:Class="PageNavigation.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid Name="page1" Background="Beige">
            <Label VerticalAlignment="Center" HorizontalAlignment="Center">Page1</Label>
        </Grid>
        <Grid Name="page2" Background="Blue" Visibility="Hidden">
            <Label VerticalAlignment="Center" HorizontalAlignment="Center">Page2</Label>
        </Grid>
        <Grid Name="page3" Background="Green" Visibility="Hidden" >
            <Label VerticalAlignment="Center" HorizontalAlignment="Center">Page3</Label>
        </Grid>
        <Grid Name="page4" Background="Cyan" Visibility="Hidden">
            <Label VerticalAlignment="Center"   HorizontalAlignment="Center">Page4</Label>
        </Grid>
        <Button Name="btn_Next" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="Next" Click="btn_Next_Click"/>
        <Button Name="btn_Previous" HorizontalAlignment="Left" VerticalAlignment="Bottom" Content="Previous" Click="btn_Previous_Click"/>
    </Grid>
</Window>

C#:

public partial class MainWindow : Window
    {
        Grid[] pages;
        int activePage = 0;
        public MainWindow()
        {

            InitializeComponent();
            pages = new Grid[4];
            pages[0] = this.page1;
            pages[1] = this.page2;
            pages[2] = this.page3;
            pages[3] = this.page4;
        }
        void Next() {
            if (activePage<pages.Length-1)
            {
                pages[activePage].Visibility = Visibility.Hidden;
                activePage++;
                pages[activePage].Visibility = Visibility.Visible;
            }    
        }
        void Previous()
        {
            if (activePage > 0)
            {
                pages[activePage].Visibility = Visibility.Hidden;
                activePage--;
                pages[activePage].Visibility = Visibility.Visible;
            }
        }

        private void btn_Previous_Click(object sender, RoutedEventArgs e)
        {
            Previous();
        }

        private void btn_Next_Click(object sender, RoutedEventArgs e)
        {
            Next();
        }
    }