Sunday, April 24, 2011

Keeping the Bottom of a Textblock Displayed, in a ScrollViewer

The following method checks to see if text in a TextBlock which is dynamically updated, wraps around, and occupies a new line. The method also invokes the ScrollToVerticalOffset method of the ScrollViewer control, if the above happens. The following are 3 versions of the method, based on suggested solutions I received:

Version 1 of method:

        private void UpdateDisplay()
        {
            // Height of text in text block
            float DataInputHeight = (float)MainAppPage_cls.DataInput.ActualHeight;
           
            if (TextPreviousHeight_cls == 0)
            {
                TextPreviousHeight_cls = DataInputHeight;
            }
            else
            {
                if (DataInputHeight > TextPreviousHeight_cls)
                {
                    MainAppPage_cls.CalculatorScreenScrollViewer.ScrollToVerticalOffset(DataInputHeight);
                    TextPreviousHeight_cls = DataInputHeight;
                }
            }
        }

Version 2 of method:


        private void UpdateDisplay()
        {
            // Height of text in text block
            float DataInputHeight = (float)MainAppPage_cls.DataInput.ActualHeight;
           
            if (TextPreviousHeight_cls == 0)
            {
                TextPreviousHeight_cls = DataInputHeight;
            }
            else
            {
                if (DataInputHeight > TextPreviousHeight_cls)
                {
                    MainAppPage_cls.CalculatorScreenScrollViewer.UpdateLayout();
                    MainAppPage_cls.CalculatorScreenScrollViewer.ScrollToVerticalOffset(MainAppPage_cls.CalculatorScreenSubCanvas.ActualHeight);
                    TextPreviousHeight_cls = DataInputHeight;
                }
            }
        }

Version 3 of method:


        private void UpdateDisplay()
        {
            // Height of text in text block
            float DataInputHeight = (float)MainAppPage_cls.DataInput.ActualHeight;
           
            if (TextPreviousHeight_cls == 0)
            {
                TextPreviousHeight_cls = DataInputHeight;
            }
            else
            {
                if (DataInputHeight > TextPreviousHeight_cls)
                {
                    MainAppPage_cls.CalculatorScreenScrollViewer.UpdateLayout();
                    MainAppPage_cls.CalculatorScreenScrollViewer.ScrollToVerticalOffset(DataInputHeight);
                    TextPreviousHeight_cls = DataInputHeight;
                }
            }
        }

No comments:

Post a Comment