UWP打开时更改组合框位置 [英] UWP change ComboBox position when opening
问题描述
我在c#XAML中有一个 ComboBox
,并且未选择任何内容并且显示了 PlaceHolderText
并单击
我希望下拉菜单在顶部打开。
假设我有一个 ComboBox
并用数字 1-100 填充,然后希望它从 1 开始显示强>。如果下拉列表中显示了七个项目,则数字 1-7 应该可见。正常行为是显示数字 47-53 。
旧的解决方法是使用 ListView
,但我不想要这个。
我该如何实现?
该解决方法如何?
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
命名空间StackOverFlowSampleApp
{
public class ExtendedComboBox:ComboBox
{
private ScrollViewer _scrollViewer;
public ExtendedComboBox()
{
DefaultStyleKey = typeof(ComboBox);
}
受保护的覆盖无效OnApplyTemplate()
{
base.OnApplyTemplate();
_scrollViewer = GetTemplateChild( ScrollViewer)作为ScrollViewer;
if(_scrollViewer!= null)
{
_scrollViewer.Loaded + = OnScrollViewerLoaded;
}
}
私有无效OnScrollViewerLoaded(对象发送者,RoutedEventArgs e)
{
_scrollViewer.Loaded-= OnScrollViewerLoaded;
_scrollViewer.ChangeView(null,0,null);
}
}
}
之前的工作方式:
解决方法后如何工作:
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:
这篇关于UWP打开时更改组合框位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!