rikkit
rikkit

Reputation: 1137

Programmatically flip through Metro FlipView

I'd like a FlipView to rotate through its items on a timer. The timer is easy, but is there a way to make the FlipView show the next item (and then loop around) in code?

The answer in this question Why does FlipView ignore SelectedItem doesn't seem to work.

Upvotes: 2

Views: 4405

Answers (5)

Ravi Ganesh
Ravi Ganesh

Reputation: 109

In Windows 10 VS2017 For the following XML code:

<FlipView Grid.Row="1" x:Name="fvMainControls">
    <FlipViewItem>
        <CheckBox Content="My Overlay" Height="21" Canvas.Left="191" Canvas.Top="164" Width="131"/>
    </FlipViewItem>
    <FlipViewItem>
        <Rectangle HorizontalAlignment="Left" Height="387" Margin="54,140,0,0" Stroke="Black" VerticalAlignment="Top" Width="206" Fill="#FF6A1717" FocusVisualPrimaryBrush="#FFA46767"/>
    </FlipViewItem>
</FlipView>

This works:

fvMainControls.SelectedIndex = 0          
fvMainControls.SelectedIndex = 1

And

fvMainControls.SelectedItem = fvMainControls.Items(0)
fvMainControls.SelectedIndex = fvMainControls.Items(1)

works. Ravi

Upvotes: 0

David Yang
David Yang

Reputation: 31

This may help in some situations

flipView.SelectedIndex = -1;
flipView.SelectedIndex = desiredIndex;

OR

flipView.SelectedItem = null;
flipView.SelectedItem = flipViewItem;

Upvotes: 2

rikkit
rikkit

Reputation: 1137

For some reason I needed to set both the SelectedIndex and SelectedItem for these FlipViews. Odd behaviour :/ (perhaps it is a change from RP to RTM - I am on RTM.)

Upvotes: 0

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

It will work if you are notifying the UI that the property changed. Your ViewModel must implement INotifyPropertyChanged and the property needs to fire the PropertyChanged event

public class ViewModel : INotifyPropertyChanged
{
    private object _selectedItem;

    public object SelectedItem
    {
        get { return _selectedItem; }
        set 
        { 
            _selectedItem = value; 
            OnPropertyChanged("SelectedItem"); 
        }
    }
}

You can also use the BindableBase class comes with the sample apps

public class ViewModel : BindableBase
{
    private object _selectedItem;

    public object SelectedItem
    {
        get { return this._selectedItem; }
        set { this.SetProperty(ref this._selectedItem, value); }
    }
}

Upvotes: 2

Glen Gordon
Glen Gordon

Reputation: 356

Just skip the SelectedItem property and use SelectedIndex instead. Set it to 0 to begin and just increase it by one until you reach the value equivalent to the number of items in the list minus one, then go back to zero. I just tested this and it works with flipview items declared in XAML or bound to the ItemsSource property.

Upvotes: 2

Related Questions