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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With