Binding Pivot SelectionChanged events to VM Command

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

I want to achieve is the page is loaded, only access to SelectedIndex=0 data, then according to the dynamic SelectionChanged Pivot access to other items of data, I use the MVVM Command way, do not want to use the registered event way to achieve. The following is a start code:

The front-end 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>


In 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());
               }));
           }
       }

This time the problem came out, When the page is first loaded Pivot.SelectIndex=0, But don't execute Command, Slide the Pivot, When the Command and Pivot.SelectIndex=1, But this time the value of X =0, That is to say after the Command, The value of X is a former PivotItem Index, Instead of the normal Pivot SelectionChanged trigger events under a PivotItem SelectIndex value. Do not know if there is no God can help answer questions?

Then think of an alternative, is a two-way binding piovt SelectIndex,

The front-end 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");
           }
       }


Today accidentally from other blogs to see another solution, is still the first kind of binding Command, but the parameters changed, directly to the 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>

Background 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;
                    }
                }));
            }
        }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Wright at December 06, 2013 - 5:06 AM