Pivot的SelectionChanged事件绑定到VM的Command
我要实现的是页面加载时,只获取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的更多相关文章
- WPF中如何将ListViewItem双击事件绑定到Command
今天的博客将介绍如何实现ListViewItem双击事件绑定到ViewModel中的Command.实现方法借助了Style中的EventSetter,请看下面的详细代码: <ListView ...
- WPF Event 在 Command 中的应用初级篇,支持所有Event 展示松耦合设计的全部代码 - 解决TextBoxBase.TextChanged或者TextBox.TextChanged等类似事件绑定问题。
做过WPF开发的人,都知道做MVVM架构,最麻烦的是Event的绑定,因为Event是不能被绑定的,同时现有的条件下,命令是无法替代Event.而在开发过程中无法避免Event事件,这样MVVM的架构 ...
- vuejs学习笔记(1)--属性,事件绑定,ajax
属性 v-for 类似于angular中的 ng-repeat ,用于重复生成html片段: <ul id="box"> <li v-for="(v, ...
- vuejs学习笔记(2)--属性,事件绑定,ajax
属性 v-for 类似于angular中的 ng-repeat ,用于重复生成html片段: <ul id="box"> <li v-for="(v, ...
- v-on事件绑定指令
v-on:事件绑定 v-on简写:@ 绑定click事件时: 代码: <script> window.onload= () =>{ let vm=new Vue({ el:'#two ...
- [Vue]组件——使用.native和$listeners将控件的原生事件绑定到组件
1.方法1:.native修饰符 1.1.native修饰符:将原生事件绑定到组件的根元素上 <base-input v-on:focus.native="onFocus"& ...
- WPF中MVVM模式下控件自有的事件绑定
1.原因 在WPF中单纯的命令绑定往往不能满足覆盖所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,这时就可以用事件绑 ...
- Vue样式绑定、事件绑定
1.样式绑定 1.1class类标签绑定 <p :class="对象"> <p :class="数组"> <p :class=&q ...
- vue样式操作与事件绑定
Vue笔记 1 Vue实例 (VM) var vm = new Vue({ el:'#app', //挂载元素 //数据 data: { title:'值', ...
随机推荐
- cocos2d-x图层以及显示对象的基本使用
LogoNode: #ifndef LogoNode_hpp #define LogoNode_hpp #include <stdio.h> #include "cocos2d. ...
- Linux 常用命令行
Linux常用命令行 第一部分: cd命令 第二部分:文件操作 第三部分:压缩包操作
- codeforces C. Triangle
C. Triangle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 制作nginx和php的rpm包
rpm包的制作真几把烦,制作php的rpm花了我3天时间,因为是根据线上环境来做的,依赖的第三方库太多,本来想把所有的第三方库做进php包,后来发现在rpmbuild -bb的时候非常耗时,而且乱七八 ...
- C++find函数
头文件 #include <algorithm> 函数实现 template<class InputIterator, class T> InputIterator find ...
- SQL SERVER 2012 使用订阅发布同步数据库
软件做大了,客户就多了,一个数据库服务器是远远不够的,当有一台数据服务器卦掉,那整个系统就会崩溃,所以必须考虑到数据库的自动同步与备份,当一台数据库服务 器宕机,自然就有用一台数据服务器启动起来保证整 ...
- Linux shell get random number
the Shell Profile: When a new interactive shell is started, /etc/profile, followed by /etc/bash.bash ...
- 怪物彈珠Monster Strike 攻略
火>水>木>光>暗 1.每天的曜日素材本,周一暗光,周二火,周参水,周四木,周五光乌龟,都是可以打整天的 2.另外补充,升经验用暗乌龟,切忌切记要塞给他随便一只烂宠升等,再吃掉 ...
- Mecanim的Retargeting和BodyMask
Retargeting(动画重定向) 文档 http://game.ceeger.com/Manual/Retargeting.html 介绍 Mecanim 的最强大的功能之一,重定目标的仿人机器人 ...
- java 20 -2 递归之找特定目录下的特定格式文件
/* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...