如何通过仅单击用户控件中的一个按钮来更新框架的Source属性?

如何解决如何通过仅单击用户控件中的一个按钮来更新框架的Source属性?

| 我想了解如何更新源属性,方法是基于点击次数仅单击一个\“ Next \”按钮,并且每次再次单击该按钮时,都可以将不同页面加载到框架中。任何建议都非常感谢!先感谢您。 主窗口代码:
<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,20\">
    <Button Content=\"Back\" HorizontalAlignment=\"Left\" Width=\"75\"/>
    <Button Content=\"Next\" HorizontalAlignment=\"Left\" Width=\"75\" />
</StackPanel>
    

解决方法

创建一个实现
NextPageCommand
PreviousPageCommand
命令的
PageViewModel
类,它们分别引发
UserNavigatedToNextPage
UserNavigatedToPreviousPage
事件。为了简单起见,还让它们公开类型为
PageViewModel
NextPage
PreviousPage
属性。为每个页面创建
PageViewModel
的子类。 为拥有的
UserControl
创建一个视图模型类,该类公开了类型为
PageViewModel
CurrentPage
属性。创建所有
PageViewModel
对象,并分别设置
NextPage
PreviousPage
。在这些对象上为导航事件添加处理程序,如下所示:
public void Page_UserNavigatedToNextPage(object sender,EventArgs e)
{
   if (sender == CurrentPage && CurrentPage.NextPage != null)
   {
      CurrentPage = CurrentPage.NextPage;
   }
}
假设您已经实现了属性更改通知,那么现在每当执行当前页面的
NextPageCommand
或ѭ4,时,
CurrentPage
属性就会更新并反映在用户界面中。如果您已经为每种浏览量模型类型创建了数据模板,那么您所需要做的就是
<ContentPresenter Content=\"{Binding CurrentPage}\"/>
在您的用户控件中,您一切顺利。 如果“上一个/下一个”按钮位于控件中,而不位于页面中,则在主视图模型中实现显示
CurrentPage.NextPageCommand
CurrentPage.PreviousPageCommand
的属性,并将这些按钮绑定到它们。     ,在您的NavUserControl中,我将为下一个和后一个按钮连接事件或命令(或者可能是两者)。然后,您可以从MainWindow中访问它们,并在Source属性中设置适当的值。 如果您选择事件路线,请附加事件并直接设置“源”。 如果执行命令路径,请在视图模型中设置命令,将其绑定到用户控件,然后将Source属性绑定到视图模型中的另一个值。 编辑:根据OP的请求添加一些代码。请记住,这并非最佳做法。只是一些例子。 走事件路线应该是最简单的。我想您已经知道如何做到这一点。只需添加:
public event EventHandler BackClicked;
public event EventHandler NextClicked;

private void Back_Click(object sender,RoutedEventArgs e)
{
    BackClicked(sender,e);
}

private void Next_Click(object sender,RoutedEventArgs e)
{
    NextClicked(sender,e);
}
事件发送到NavUserControl。然后将您的XAML更改为:
<StackPanel x:Name=\"LayoutRoot\" Orientation=\"Horizontal\" VerticalAlignment=\"Bottom\" HorizontalAlignment=\"Center\" Margin=\"0,20\">    
    <Button Content=\"Back\" HorizontalAlignment=\"Left\" Width=\"75\" Click=\"Back_Click\" />    
    <Button Content=\"Next\" HorizontalAlignment=\"Left\" Width=\"75\" Click=\"Next_Click\" />
</StackPanel>
现在在您的MainWindow.xaml.cs文件中,添加:
private void BackClicked(object sender,EventArgs e)
{
    Uri source = // Whatever your business logic is to determine the previous page;
    _Frame.Source = source;
}

private void NextClicked(object sender,EventArgs e)
{
    Uri source = // Whatever your business logic is to determine the next page;
    _Frame.Source = source;
}
并将MainWindow XAML更改为:
<Grid x:Name=\"LayoutRoot\">
    <Frame x:Name=\"_Frame\" Content=\"Frame\" 
           Source=\"/WpfApplication1;component/Page1.xaml\"/>
    <local:NavUserControl HorizontalAlignment=\"Center\" VerticalAlignment=\"Bottom\" 
                          BackClicked=\"BackClicked\" NextClicked=\"NextClicked\" />
</Grid>
沿命令路线走会花费更多的架构,但要干净得多。我建议使用您最喜欢的MVVM工具包。我最喜欢的是MVVMLight,这就是我在此示例中使用的。 创建一个ViewModel类,如下所示:
public class ViewModel : GalaSoft.MvvmLight.ViewModelBase
{
    private Uri _Source;
    public Uri Source
    {
        get { return _Source; }
        set
        {
            if (_Source != value)
            {
                _Source = value;
                RaisePropertyChanged(\"Source\");
            }
        }
    }



    private GalaSoft.MvvmLight.Command.RelayCommand _BackCommand;
    public ICommand BackCommand
    {
        get
        {
            if (_BackCommand == null)
            {
                _BackCommand = new GalaSoft.MvvmLight.Command.RelayCommand(() =>
                {
                    Uri source = // Whatever your business logic is to determine the previous page
                    Source = source;
                });
            }
            return _BackCommand;
        }
    }

    private GalaSoft.MvvmLight.Command.RelayCommand _NextCommand;
    public ICommand NextCommand
    {
        get
        {
            if (_NextCommand == null)
            {
                _NextCommand = new GalaSoft.MvvmLight.Command.RelayCommand(() =>
                {
                    Uri source = // Whatever your business logic is to determine the next page
                    Source = source;
                });
            }
            return _NextCommand;
        }
    }
}
在您的MainWindow.xaml.cs中,创建此类的实例,并将您的DataContext属性设置为该实例。然后设置绑定:
<Grid x:Name=\"LayoutRoot\">    
    <Frame Content=\"Frame\" Source=\"{Binding Source}\"/>    
    <local:NavUserControl HorizontalAlignment=\"Center\" VerticalAlignment=\"Bottom\"/>
</Grid>
<StackPanel x:Name=\"LayoutRoot\" Orientation=\"Horizontal\" VerticalAlignment=\"Bottom\" HorizontalAlignment=\"Center\" Margin=\"0,20\">    
    <Button Content=\"Back\" HorizontalAlignment=\"Left\" Width=\"75\" Command=\"{Binding BackCommand}\"/>    
    <Button Content=\"Next\" HorizontalAlignment=\"Left\" Width=\"75\" Command=\"{Binding NextCommand}\" />
</StackPanel>
绑定示例是非常简单的MVVM样式的WPF。我建议您采用这种方法,如果需要更多帮助,请继续阅读WPF中的MVVM。教程和书籍形式的大量资源。在SO上搜索这里也有很大帮助。 再次编辑: 将您的构造函数更改为此:
public MainWindow()
{
    this.InitializeComponent();

    // Insert code required on object creation below this point.
    DataContext = new ViewModel();
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-