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:'值', ...
随机推荐
- Web Service中的几个重要术语
WSDL:web service definition language 直译:WebService定义语言 1.对应一种该类型的文件.WSDL 2.定义了Web Service的服务器与客户端应用交 ...
- jenkins 更换主数据目录
工作中,由于Jenkins默认的主目录空间太小,导致需要将Jenkins默认的主目录修改到其它目录.本文针对更改Jenkins的主目录详细介绍. 注意:在Jenkins运行时是不能更改的. 请先将Je ...
- android linker (1) —— __linker_init()
ilocker:关注 Android 安全(新手) QQ: 2597294287 __linker_init() 在 begin.S 中被调用,并传入两个参数:sp(堆栈指针).#0. linker( ...
- 免费微信公众号专用h5在线电影票API
免费h5在线电影票API,通过嵌套返回的h5页面url,实现电影票购买. 接口文档:https://www.juhe.cn/docs/api/id/252,通过此申请APPKEY 接口备注:通过请求返 ...
- JVM 运行时数据区域
Java虚拟机管理的内存包括以下几个运行时数据区域: 1.程序计数器: 程序计数器是一块比较小的内存空间,是当前线程执行的字节码行号指示器.Java多线程是通过线程轮流切换来实现的,所以每个线程都有一 ...
- A Personal Selection of Books on E lectromagnetics and Computational E lectromagnetics---David B. Davidson
链接. General Books on Electromagnetics When our department recently reviewed our junior-level text, w ...
- runv start container 流程分析
1.runv/start.go func startContainer(context *cli.Context, container, address string, config *spec.Sp ...
- selenium使用等待的几种方式
1.使用java的sleep try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated c ...
- 第25章 SEH结构化异常处理_未处理异常及向量化异常
25.1 UnhandledExceptionFilter函数详解 25.1.1 BaseProcessStart伪代码(Kernel32内部) void BaseProcessStart(PVOID ...
- 解决Gradle DSL method not found: ‘android()’
最近导入as的项目出了这样的问题 这个问题困扰了我很长时间,好吧,搜了半天全都是runProguard的,最后在stackoverflow上搜到解决办法了: http://stackoverflow. ...