Coding Bots
Coding Bots

Reputation: 41

How to check that which item is selected currently in Flip view in XAML C#

How can i check that which item of the flipview is currently selected and i want different action in code behind.

For Example

here is the code

<FlipView SelectionChanged="FlipView_SelectionChanged">
      <Stackpanel Name="sp1">
        // Stack panel 1
      </Stackpanel>
      <Stackpanel Name="sp2">
        // Stack panel 2
      </Stackpanel>
 </FlipView>

My question is how i know that "sp1" is selected currently or "sp2" is selected in code behing? any event or other method? or in other words who can i know that user flipped from one view to the next view
Any response will be highly appreciated.

Upvotes: 1

Views: 1857

Answers (1)

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

Give the FlipView a Name then you can reference the SelectedItem

XAML


<FlipView Name="myFlipView" SelectionChanged="FlipView_SelectionChanged">
      <Stackpanel Name="sp1">
        // Stack panel 1
      </Stackpanel>
      <Stackpanel Name="sp2">
        // Stack panel 2
      </Stackpanel>
 </FlipView>

C#


// get selected index/item
StackPanel sp = (StackPanel) myFlipView.SelectedItem;
int selected_index = myFlipView.SelectedIndex;
string name_of_selected_panel = sp.Name;

// set selected index/item
myFlipView.SelectedIndex = 1;  // any valid index
myFlipView.SelectedItem = sp1;  // or any name of an item in the collection

Upvotes: 2

Related Questions