6

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?

user3079834
  • 2,009
  • 2
  • 31
  • 63

1 Answers1

16

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

Andrii Krupka
  • 4,276
  • 3
  • 20
  • 41