UWP change ComboBox position when opening

邮差的信 提交于 2019-12-23 12:15:57

问题


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?


回答1:


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:

How it works after workaround:



来源:https://stackoverflow.com/questions/35913824/uwp-change-combobox-position-when-opening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!