0
votes

Is there a property that I can bind to on the Hub control in Windows 8.1 / Windows Phone 8.1 such that I can get the currently selected HubSection? Normally for a TabControl, one would set the SelectedIndex property. However this does not seem to exist for the Hub control.

So far the only property I can find that is somewhat related is the SectionsInView property. However it is read only and it can't be bound to via DataBinding.

enter image description here

1
How are you trying to bind? It is a vector. And despite that I could data bind to it from a TextBlocKamen

1 Answers

2
votes

Find a way to deal with that but a shame that there is no native property for it.

So what I did is :

        private int CurrentSectionIndex = 0;
        private HubSection CurrentSection
        {
            get { return this.Hub.SectionsInView[CurrentSectionIndex]; }
        }

        //Retrieve the ScrollViewer of the Hub control and handle the ViewChanged event of this ScrollViewer
        public Page()
        {
            ScrollViewer sv = ControlHelper.GetChild<ScrollViewer>(this.Hub);
            sv.ViewChanged += sv_ViewChanged;
        }
        //In the ViewChanged event handler body then calculate the index relative to the horizontal offset (means current position) of the ScrollViewer
        void sv_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            if(!e.IsIntermediate)
            {
                double ho = ControlHelper.GetChild<ScrollViewer>(this.Hub).HorizontalOffset;

                if (ho >= CurrentSection.ActualWidth - (this.ActualWidth - CurrentSection.ActualWidth))
                {
                    CurrentSectionIndex = (int)((ho + (this.ActualWidth - CurrentSection.ActualWidth)) / CurrentSection.ActualWidth);
                }
            }
        }

Seems to work but still to test (let me know)