Windows Presentation Foundation
.NET Framework 的一部分,它提供统一的编程模型,用于在 Windows 上构建业务线桌面应用程序。
128 个问题
关于Windows Presentation Foundation的自定义导航栏开发导航功能问题
在WPF(Windows Presentation Foundation)中,有几种常用的导航方式。
NavigationWindow 和 Page:
Page
页面。Frame 和 Page:
Frame
控件来承载 Page
页面。ContentControl 和 UserControl:
ContentControl
结合 UserControl
实现导航。下面是一个简单的示例。
MainWindow.xaml:
<Window x:Class="WpfApp4.MainWindow"
...
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:NavigationViewModel />
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Frame Name="MainFrame" Source="HomePage.xaml" />
<Button Grid.Column="1" Content="navigate window" Height="40" Click="Button_Click"/>
<ContentControl Grid.Column="2" Name="MainContentControl" />
<StackPanel Orientation="Horizontal" Grid.Column="3">
<Button Content="Home" Height="40" Command="{Binding NavigateCommand}" CommandParameter="Home"/>
<Button Content="Next" Height="40" Command="{Binding NavigateCommand}" CommandParameter="Next"/>
</StackPanel>
<Frame Grid.Column="3" Content="{Binding CurrentPage}" />
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
NavigateToNextPage();
ShowNextUserControl();
}
private void NavigateToNextPage()
{
MainFrame.Navigate(new Uri("NextPage.xaml", UriKind.Relative));
}
private void ShowNextUserControl()
{
MainContentControl.Content = new NextUserControl();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationWindow navigationWindow = new NavigationWindow();
navigationWindow.Source = new Uri("Page1.xaml", UriKind.Relative);
navigationWindow.Show();
}
}
public class NavigationViewModel : INotifyPropertyChanged
{
private ICommand _navigateCommand;
private Page _currentPage;
public Page CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
OnPropertyChanged("CurrentPage");
}
}
public ICommand NavigateCommand
{
get
{
if (_navigateCommand == null)
{
_navigateCommand = new RelayCommand<string>(Navigate);
}
return _navigateCommand;
}
}
private void Navigate(string pageName)
{
switch (pageName)
{
case "Home":
CurrentPage = new HomePage();
break;
case "Next":
CurrentPage = new NextPage();
break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class RelayCommand<T> : ICommand
{
private readonly Action<T> _execute;
private readonly Func<T, bool> _canExecute;
public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null || _canExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
Page1:
<StackPanel>
<TextBlock Text="Page1" />
<Button Content="Go to Next Page" Click="GoToNextPage_Click"/>
</StackPanel>
// Page1.xaml.cs:
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void GoToNextPage_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("NextPage.xaml", UriKind.Relative));
}
}
HomePage:
<StackPanel>
<TextBlock Text="HomePage" />
<Button Content="Go to Next Page" Click="GoToNextPage_Click"/>
</StackPanel>
// HomePage .xaml.cs:
public partial class HomePage : Page
{
public HomePage()
{
InitializeComponent();
}
private void GoToNextPage_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("NextPage.xaml", UriKind.Relative));
}
}
Nextpage:
<Grid>
<TextBlock Text="nextpage" />
</Grid>
NextUserControl:
<Grid>
<TextBlock Text="NextUserControl" />
</Grid>
如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。
注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。