wpf 使用按钮导航到导航窗口中的另一个页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/803921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a Button to navigate to another Page in a NavigationWindow
提问by
I'm trying to use the navigation command framework in WPF to navigate between Pages within a WPF application (desktop; not XBAP or Silverlight).
我正在尝试使用 WPF 中的导航命令框架在 WPF 应用程序(桌面;不是 XBAP 或 Silverlight)中的页面之间导航。
I believe I have everything configured correctly, yet its not working. I build and run without errors, I'm not getting any binding errors in the Output window, but my navigation button is disabled.
我相信我已经正确配置了所有内容,但它无法正常工作。我构建和运行没有错误,在输出窗口中没有收到任何绑定错误,但我的导航按钮被禁用。
Here's the app.xaml for a sample app:
这是示例应用程序的 app.xaml:
<Application x:Class="Navigation.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="First.xaml">
</Application>
Note the StartupUri points to First.xaml. First.xaml is a Page. WPF automatically hosts my page in a NavigationWindow. Here's First.xaml:
请注意 StartupUri 指向 First.xaml。First.xaml 是一个页面。WPF 自动将我的页面托管在 NavigationWindow 中。这是 First.xaml:
<Page x:Class="Navigation.First"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="First">
<Grid>
<Button
CommandParameter="/Second.xaml"
CommandTarget="{Binding RelativeSource=
{RelativeSource
FindAncestor,
AncestorType={x:Type NavigationWindow}}}"
Command="NavigationCommands.GoToPage"
Content="Go!"/>
</Grid>
</Page>
The button's CommandTarget is set to the NavigationWindow. The command is GoToPage, and the page is /Second.xaml. I've tried setting the CommandTarget to the containing Page, the CommandParameter to "Second.xaml" (First.xaml and Second.xaml are both in the root of the solution), and I've tried leaving the CommandTarget empty. I've also tried setting the Path to the Binding to various navigational-related public properties on the NavigationWindow. Nothing has worked so far.
按钮的 CommandTarget 设置为 NavigationWindow。命令为 GoToPage,页面为 /Second.xaml。我尝试将 CommandTarget 设置为包含的页面,将 CommandParameter 设置为“Second.xaml”(First.xaml 和 Second.xaml 都在解决方案的根目录中),并且我尝试将 CommandTarget 留空。我还尝试将 Path to the Binding 设置为 NavigationWindow 上各种与导航相关的公共属性。到目前为止,一切都没有奏效。
What am I missing here? I reallydon't want to do my navigation in code.
我在这里缺少什么?我真的不想在代码中进行导航。
Clarification.
澄清。
If, instead of using a button, I use a Hyperlink:
如果我不使用按钮,而是使用超链接:
<Grid>
<TextBlock>
<Hyperlink
NavigateUri="Second.xaml">Go!
</Hyperlink>
</TextBlock>
</Grid>
everything works as expected. However, my UI requirements means that using a Hyperlink is right out. I need a big fatty button for people to press. That's why I want to use the button to navigate. I just want to know how I can get the Button to provide the same ability that the Hyperlink does in this case.
一切都按预期工作。但是,我的 UI 要求意味着使用超链接是正确的。我需要一个大按钮供人们按下。这就是为什么我想使用按钮进行导航。我只想知道如何让按钮提供与超链接在这种情况下相同的功能。
采纳答案by Andy
According to the documentation, only DocumentViewer
and FlowDocumentViewer
implement this command specifically. You'll need to either find a command for navigation that NavigationWindow
implements, or set up a CommandBinding
for this command and handle it yourself.
根据该文件,只有DocumentViewer
和FlowDocumentViewer
具体执行此命令。您需要找到一个实现导航的命令NavigationWindow
,或者CommandBinding
为此命令设置一个并自行处理。
回答by Nate Noonen
In XAML:
在 XAML 中:
<Button Command="{x:Static Views:Commands.NavigateHelp}" Content="Help"/>
In Views (We have a Commands.cs
file that contains all of these):
在视图中(我们有一个Commands.cs
包含所有这些的文件):
public static RoutedCommand NavigateHelp = new RoutedCommand();
In the Page contstructor, you can connect the two:
在 Page 构造函数中,您可以将两者连接起来:
CommandBindings.Add(new CommandBinding(Commands.NavigateHelp, NavigateHelpExecute));
NavigateHelpExecute can be in the code behind (which is what we do), hook into a ViewModel event handler, or whatever. The beauty of this is that you can disable other navigation like so:
NavigateHelpExecute 可以在后面的代码中(这就是我们所做的),挂钩到 ViewModel 事件处理程序,或其他任何东西。这样做的好处是您可以禁用其他导航,如下所示:
CommandBindings.Add(new CommandBinding(NavigationCommands.Refresh, null));
Hope this helps.
希望这可以帮助。
回答by lkg
You will want to use the NavigationService
of your NavigationWindow
as follows:
你会想使用NavigationService
你NavigationWindow
的如下:
XAML:
XAML:
<Button HorizontalAlignment="Right" Name="continueButton" Width="75" Margin="0,0,8,11" Height="23" VerticalAlignment="Bottom" Click="continueButton_Click">
Continue
</Button>
C#:
C#:
private void continueButton_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.GoForward();
//or
this.NavigationService.Navigate("Second.xaml")
}
With either of this you can use use this
, I only show the NavigationService
here for clarity
您可以使用其中任何一个使用 use this
,NavigationService
为了清楚起见,我只在此处显示
回答by Wouter Schut
public class NavigateButton : Button
{
public Uri NavigateUri { get; set; }
public NavigateButton()
{
Click += NavigateButton_Click;
}
void NavigateButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var navigationService = NavigationService.GetNavigationService(this);
if (navigationService != null)
navigationService.Navigate(NavigateUri);
}
}
And then you can put the following in your xaml:
然后您可以将以下内容放入您的 xaml 中:
<local:NavigateButton Content="NavigateButton" NavigateUri="Page2.xaml"/>
<local:NavigateButton Content="NavigateButton" NavigateUri="Page2.xaml"/>
Then you still don't need code behind your pages, and you don't need to add commands to your viewmodel.
然后您仍然不需要页面背后的代码,也不需要向视图模型添加命令。