【发布时间】:2016-03-04 10:17:28
【问题描述】:
我的 Telerik Combobox 有问题,它也可以为基本的 WPF Combobox 复制。
我有以下情况:当用户试图打开组合框时,整个页面(包括这个控件)应该向上移动,因为在底部的空间将被键盘占用。
为了做到这一点,我捕获了 GotFocus 事件,并以编程方式更新了 Scrollviewer 的位置。
这里是 XAML:
<ScrollViewer Grid.Row="2" x:Name="KeyBoardScrollViewer" PanningMode="VerticalOnly" VerticalScrollBarVisibility="Hidden">
<telerik:RadComboBox Grid.Row="5" GotFocus="UIElement_OnGotFocus" LostFocus="UIElement_OnLostFocus"
IsEditable="True"
Grid.Column="1"
ItemsSource="{Binding StreetOthers, Mode=TwoWay}"
Text="{Binding SelectedStreetOthers, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Height="36" Width="250"
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontSize="18"
Margin="10 0 0 0"
Padding="5" />
</ScrollViewer>
还有后面的代码:
public double RememberedPoisitionOfScrollBar { get; set; }
private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
{
Application.Current.Dispatcher.InvokeAsync(() =>
{
if (!Keyboard.IsOpen)
{
Keyboard.IsOpen = true;
KeyBoardScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
}
Point relativePoint = ((FrameworkElement)sender).TransformToAncestor(KeyBoardScrollViewer)
.Transform(new Point(0, 0));
KeyBoardScrollViewer.CanContentScroll = false;
RememberedPoisitionOfScrollBar = relativePoint.Y - 5;
var offset = KeyBoardScrollViewer.VerticalOffset + relativePoint.Y - 5; //sender.OccludedRect.Top
KeyBoardScrollViewer.ScrollToVerticalOffset(offset);
//KeyBoardScrollViewer.ScrollToVerticalOffset(offset);
KeyBoardScrollViewer.UpdateLayout();
});
Keyboard.Width = this.ActualWidth;
}
private void UIElement_OnLostFocus(object sender, RoutedEventArgs e)
{
Keyboard.IsOpen = false;
KeyBoardScrollViewer.ScrollToVerticalOffset(KeyBoardScrollViewer.VerticalOffset - RememberedPoisitionOfScrollBar);
KeyBoardScrollViewer.UpdateLayout();
KeyBoardScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
此解决方案有效,组合框移动到视图顶部,但我有一个奇怪的行为。组合框“内容”保持在原来的位置,不会根据组合框的新位置进行更新。 这是图片:
根据 Telerik 管理员 (old post) 的说法,这是一个 WPF 问题,但它必须是一种解决方法,不是吗?任何想法都是有用的!
谢谢!
【问题讨论】: