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 0:
                            break;
                        case 1:
                            break;
                        case 2:
                            break;
                    }
                }));
            }
        }

 

posted on 2013-08-23 15:31  灬番茄  阅读(584)  评论(0编辑  收藏  举报

导航