我要实现的是页面加载时,只获取SelectedIndex=0的数据,然后根据Pivot的SelectionChanged动态获取其他项的数据,我用的是MVVM的Command的方式,不想用后台注册事件的方式来实现。下面的是一开始的代码:

前端xaml:

<phone:Pivot x:Name="pivot">
            <phone:Pivot.Title>
                <TextBlock Text="{Binding Path=PageName}"/>
            </phone:Pivot.Title>
         <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
                    <Command:EventToCommand Command="{Binding GetFlightInfoCommand}" CommandParameter="{Binding Path=SelectedIndex, ElementName=pivot}"/>
          </i:EventTrigger>
         </i:Interaction.Triggers>

</phone:Pivot>

VM中的Command:

private RelayCommand<int> getFlightInfoCommand;
        public RelayCommand<int> GetFlightInfoCommand
        {
            get
            {
                return getFlightInfoCommand ?? (getFlightInfoCommand = new RelayCommand<int>((x) =>
                {
                    this.GetFlightInfo(this._landType, this._upDown, PivotSelectedIndex.ToString());
                }));
            }
        }

这时候问题就出来了,页面首次加载时Pivot.SelectIndex=0,但是不会执行Command,向右滑动Pivot,这时会执行这个Command并且Pivot.SelectIndex=1,但是这时候x的值=0,也就是说执行Command后,x的值是前一个PivotItem的Index,而不是正常的触发Pivot的SelectionChanged事件的下一个PivotItem的SelectIndex值。不知道有没有大神可以帮忙解惑?

后来想到个变通的办法,就是双向绑定piovt的SelectIndex,

前端xaml:

<phone:Pivot x:Name="pivot" SelectedIndex="{Binding Path= PivotSelectedIndex,Mode=TwoWay}">

</phone:Pivot>

ViewModel:

public int PivotSelectedIndex
        {
            get
            {
                return pivotSelectedIndex;
            }
            set
            {
                if (pivotSelectedIndex != value)
                {
                    this.GetFlightInfo(this._landType, this._upDown, value.ToString());
                }
                pivotSelectedIndex = value;
                RaisePropertyChanged("PivotSelectedIndex");
            }
        }

今天无意中从其他的博客中看到另外一个解决办法,仍然是第一种的绑定Command的方法,只不过参数改变了,直接把Pivot传进去:

<phone:Pivot x:Name="pivot">
<phone:Pivot.Title>
<TextBlock Text="{Binding Path=PageName}"/>
</phone:Pivot.Title>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<Command:EventToCommand Command="{Binding GetFlightInfoCommand}" CommandParameter="{Binding ElementName=pivot}"/>
</i:EventTrigger>
</i:Interaction.Triggers> </phone:Pivot>

后台Command:

 public RelayCommand<Pivot> SelectionChangedCommand
{
get
{
return selectionChangedCommand ?? (selectionChangedCommand = new RelayCommand<Pivot>(x =>
{
if (x == null)
return;
switch (x.SelectedIndex)
{
case :
break;
case :
break;
case :
break;
}
}));
}
}

Pivot的SelectionChanged事件绑定到VM的Command的更多相关文章

  1. WPF中如何将ListViewItem双击事件绑定到Command

    今天的博客将介绍如何实现ListViewItem双击事件绑定到ViewModel中的Command.实现方法借助了Style中的EventSetter,请看下面的详细代码: <ListView ...

  2. WPF Event 在 Command 中的应用初级篇,支持所有Event 展示松耦合设计的全部代码 - 解决TextBoxBase.TextChanged或者TextBox.TextChanged等类似事件绑定问题。

    做过WPF开发的人,都知道做MVVM架构,最麻烦的是Event的绑定,因为Event是不能被绑定的,同时现有的条件下,命令是无法替代Event.而在开发过程中无法避免Event事件,这样MVVM的架构 ...

  3. vuejs学习笔记(1)--属性,事件绑定,ajax

    属性 v-for 类似于angular中的 ng-repeat ,用于重复生成html片段: <ul id="box"> <li v-for="(v, ...

  4. vuejs学习笔记(2)--属性,事件绑定,ajax

    属性 v-for 类似于angular中的 ng-repeat ,用于重复生成html片段: <ul id="box"> <li v-for="(v, ...

  5. v-on事件绑定指令

    v-on:事件绑定 v-on简写:@ 绑定click事件时: 代码: <script> window.onload= () =>{ let vm=new Vue({ el:'#two ...

  6. [Vue]组件——使用.native和$listeners将控件的原生事件绑定到组件

    1.方法1:.native修饰符 1.1.native修饰符:将原生事件绑定到组件的根元素上 <base-input v-on:focus.native="onFocus"& ...

  7. WPF中MVVM模式下控件自有的事件绑定

    1.原因 在WPF中单纯的命令绑定往往不能满足覆盖所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,这时就可以用事件绑 ...

  8. Vue样式绑定、事件绑定

    1.样式绑定 1.1class类标签绑定 <p :class="对象"> <p :class="数组"> <p :class=&q ...

  9. vue样式操作与事件绑定

    Vue笔记 1 Vue实例 (VM) var vm = new Vue({    el:'#app', //挂载元素        //数据    data: {        title:'值', ...

随机推荐

  1. jQuery form插件的使用--ajaxForm()和ajaxSubmit()的可选参数项对象

    一.前提说明 Form Plugin API 里提供了很多有用的方法可以让你轻松的处理表单里的数据和表单的提交过程. 测试环境:部署到Tomcat中的web项目. 二.简单介绍 本文演示的是:jQue ...

  2. webstorm 注册码

    User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3gmjJ8xoTPw5m ...

  3. JS高级程序设计2nd部分知识要点3

    对象转换方法:1> toLocaleString(),2> toString(),ValueOf()方法会返回相同的值 栈方法是 LIFO (后进先出)的数据结构 -push ,pop 方 ...

  4. db2 常用命令(一)

    DB2数据库常用命令小结   ========操作数据库命令==========   -- 启动数据库实例 #db2start    -- 停止数据库实例    #db2sto         # 如 ...

  5. linux timezone

    首先需要了解下“UTC时间”与“本地时间”UTC时间:世界协调时间(又称世界标准时间.世界统一时间),在一般精度要求下,它与GMT(Greenwich Mean Time,格林威治标准时间)是一样的, ...

  6. 匈牙利算法 cojs.tk 搭配飞行员

    cojs.tk  搭配飞行员 ★★☆   输入文件:flyer.in   输出文件:flyer.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     飞行大队有若干个 ...

  7. python中的深拷贝和潜拷贝

    >>> a = ['ace',['age',10]] >>> b = a[:] >>> c = list(a) >>> for ...

  8. 转: EclipseIDE开发 for C++

    Eclipse 开发C++ 程序 http://tangmingjie2009.iteye.com/blog/2088363 Eclipse 开发C++ 程序 (二) 静态库 http://tangm ...

  9. 运维工作中sed常规操作命令梳理

    sed是一个流编辑器(stream editor),一个非交互式的行编辑器.它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间",接着用sed命令处理缓冲 ...

  10. Java 操作 Redis 高级

    /Users/sherry/WorkPath/Git/Web/redisDemo/src/main/java/org/zln/utils/JedisUtils.java package org.zln ...