如何通过仅单击用户控件中的一个按钮来更新框架的 Source 属性?
我想了解如何通过根据点击计数仅单击一个“下一步”按钮来更新源属性,并能够在每次单击该按钮时将不同的页面加载到框架中。任何建议都将受到高度赞赏!先感谢您。
主窗口代码:
<Grid x:Name="LayoutRoot">
<Frame Content="Frame" Source="/WpfApplication1;component/Page1.xaml"/>
<local:NavUserControl HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</Grid>
包含按钮的用户控件:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20">
<Button Content="Back" HorizontalAlignment="Left" Width="75"/>
<Button Content="Next" HorizontalAlignment="Left" Width="75" />
</StackPanel>
I’d like to find out about how to update a source property by clicking only one "Next" button based on a click count and being able to load different pages into frame each time the button is clicked another time. Any advice is highly appreciated! Thank you in advance.
Main Window Code:
<Grid x:Name="LayoutRoot">
<Frame Content="Frame" Source="/WpfApplication1;component/Page1.xaml"/>
<local:NavUserControl HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
</Grid>
User control that contains the button:
<StackPanel x:Name="LayoutRoot" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20">
<Button Content="Back" HorizontalAlignment="Left" Width="75"/>
<Button Content="Next" HorizontalAlignment="Left" Width="75" />
</StackPanel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个实现
NextPageCommand
和PreviousPageCommand
命令的PageViewModel
类,这些命令分别引发UserNavieratedToNextPage
和UserNavieratedToPreviousPage
事件。为了简单起见,还让它们公开PageViewModel
类型的NextPage
和PreviousPage
属性。为每个页面创建PageViewModel
的子类。为拥有的
UserControl
创建一个视图模型类,该类公开PageViewModel
类型的CurrentPage
属性。创建所有PageViewModel
对象,并在每个对象上设置NextPage
和PreviousPage
。在这些对象上添加导航事件的处理程序,如下所示:假设您已经实现了属性更改通知,现在每当当前页面的
NextPageCommand
或PreviousPageCommand
执行时,CurrentPage
属性将更新并反映在 UI 中。如果您已经为每种页面视图模型类型创建了数据模板,那么您所需要的一切都在您的用户控件中,您就可以开始了。
如果“下一个”/“上一个”按钮在您的控件中,而不是在页面中,则在主视图模型中实现公开
CurrentPage.NextPageCommand
和CurrentPage.PreviousPageCommand
的属性,并且将按钮绑定到它们。Create a
PageViewModel
class that implementsNextPageCommand
andPreviousPageCommand
commands, which raise (respectively)UserNavigatedToNextPage
andUserNavigatedToPreviousPage
events. To make it simple, also have them exposeNextPage
andPreviousPage
properties of typePageViewModel
. Create subclasses ofPageViewModel
for each page.Create a view model class for the owning
UserControl
that exposes aCurrentPage
property of typePageViewModel
. Create all of thePageViewModel
objects and setNextPage
andPreviousPage
on each. Add handlers for the navigation events on these object that look something like:Assuming that you've implemented property-change notification, now whenever the current page's
NextPageCommand
orPreviousPageCommand
executes, theCurrentPage
property will be updated and will be reflected in the UI. If you've created a data template for each page view model type, all you need isin your user control and you're good to go.
If the Next/Previous buttons are in your control, and not in the page, then implement properties in the main view model that expose
CurrentPage.NextPageCommand
andCurrentPage.PreviousPageCommand
, and bind the buttons to them.在您的 NavUserControl 中,我将为下一个和后退按钮连接事件或命令(或两者)。然后,您可以从 MainWindow 内访问这些内容,并将适当的值设置到 Source 属性中。
如果您选择事件路线,请附加到事件并直接设置源。
如果您采用命令路线,请在视图模型中设置一个命令,将其绑定到用户控件,并将 Source 属性绑定到视图模型中的另一个值。
编辑:根据OP的请求添加一些代码。请记住,这并不是最佳实践。只是一些例子。
走活动路线应该是最简单的。我想你已经知道如何做到这一点了。只需添加:
事件到您的 NavUserControl。然后将您的 XAML 更改为:
现在在 MainWindow.xaml.cs 文件中添加:
并将 MainWindow XAML 更改为:
使用命令路径需要更多的架构设计,但更干净。我建议使用您最喜欢的 MVVM 工具包。我最喜欢的是 MVVMLight,因此我将在本示例中使用它。
创建一个 ViewModel 类,如下所示:
在 MainWindow.xaml.cs 中,创建此类的实例并将 DataContext 属性设置为该实例。然后设置您的绑定:
并且
绑定示例是非常简单的 MVVM 风格的 WPF。我建议您走这条路,如果您需要更多帮助,请阅读 WPF 中的 MVVM。有大量教程和书籍形式的资源。 在这里搜索 SO 也有很大帮助。
再次编辑:
将构造函数更改为:
In your NavUserControl, I would wire up either events or commands (or both, perhaps) for the next and back buttons. Then you can access those from within the MainWindow and set the appropriate value into the Source property.
If you go the event route, attach onto the events and set the Source directly.
If you go the command route, setup a command in your viewmodel, bind it to the usercontrol, and bind the Source property to another value in your viewmodel.
Edit: Adding some code per the OP's request. Keep in mind, this is not intended to be best practices. Just some examples.
To go the event route should be the simplest. You already know how to do this, I'd imagine. Just add:
events to your NavUserControl. Then change your XAML to:
And now in your MainWindow.xaml.cs file, add:
and change the MainWindow XAML to be:
Going the command route takes a little more architecting, but is a lot more clean. I'd recommend using your favorite MVVM toolkit. My favorite is MVVMLight, so that's what I'll use for this example.
Create a ViewModel class, something like this:
In your MainWindow.xaml.cs, create an instance of this class and set your DataContext property to that instance. Then setup your bindings:
and
The binding example is pretty straight-forward MVVM-style WPF. I'd suggest you go that route and if you need more help, go read up on MVVM in WPF. Lots of resources out there in the form of tutorials and books. Searching here on SO can help a lot as well.
Edit again:
Change your constructor to this: