Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP change ComboBox position when opening

I have a ComboBox in c# XAML and when nothing is selected and the PlaceHolderText is shown and I click on it to open, the normal behavior is to open it in the middle.

I want the dropdown to open on top instead. Let's say I have a ComboBox and fill it with the number 1-100, then I want it to display beginning from 1. If there are seven items shown in the dropdown, then the numbers 1-7 should be visible. Normal behavior would be showing the numbers 47-53.

An old workaround would be using a ListView, but I don't want this.

How can I achieve this?

like image 471
user3079834 Avatar asked Mar 10 '16 10:03

user3079834


1 Answers

What about this workaround?

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace StackOverFlowSampleApp
{
    public class ExtendedComboBox : ComboBox
    {
        private ScrollViewer _scrollViewer;

        public ExtendedComboBox()
        {
            DefaultStyleKey = typeof(ComboBox);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _scrollViewer = GetTemplateChild("ScrollViewer") as ScrollViewer;
            if (_scrollViewer != null)
            {
                _scrollViewer.Loaded += OnScrollViewerLoaded;
            }
        }

        private void OnScrollViewerLoaded(object sender, RoutedEventArgs e)
        {
            _scrollViewer.Loaded -= OnScrollViewerLoaded;
            _scrollViewer.ChangeView(null, 0, null);
        }
    }
}

How it works before:

enter image description here

How it works after workaround:

enter image description here

like image 148
Andrii Krupka Avatar answered Oct 05 '22 23:10

Andrii Krupka