Scrollviewer中的WPF组合框无法正常工作

时间:2016-03-04 10:17:28

标签: c# wpf combobox telerik

我有一个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;

        }

此解决方案有效,组合框移动到视图的顶部,但我有一个奇怪的行为。组合框&#34;内容&#34;保持在原始位置,并且不根据组合框的新位置更新。 这是图片: enter image description here

根据Telerik管理员(old post)的说法,这是一个WPF问题,但它必须是一种解决方法,不是吗?任何想法都很有用!

谢谢!

1 个答案:

答案 0 :(得分:2)

在滚动查看器中,订阅ScrollChanged事件:

ScrollChanged="KeyboardScrollView_OnScrollChanged"

并且在代码隐藏中,如果在滚动发生时打开下拉列表,请关闭它并重新打开它:

private void KeyboardScrollView_OnScrollChanged(object sender, ScrollChangedEventArgs e)
{
  if (TheComboBox.IsDropDownOpen)
  {
    TheComboBox.IsDropDownOpen = false;
    TheComboBox.IsDropDownOpen = true;
  }
}