在表单加载时,RadPanelBar Scroll移至底部

时间:2019-02-08 05:55:09

标签: c# wpf mvvm telerik

在我的WPF应用程序中,我的RadPanelBar中有多个RadPanelItems,并且在加载表单后,所有面板项都将打开。

但是有时滚动条会移到底部,这使得最后一个PanelItem可见,而不是第一个RadPanelItem

XAML:

 <telerik:RadPanelBar Name="panelScroll" Grid.Row="0" VerticalAlignment="Stretch"  telerik:StyleManager.Theme="Windows8" ExpandMode="Multiple">
//5 RadPanelItem
 </telerik:RadPanelBar>

现在,我需要确保滚动条位于表单加载的顶部。

我尝试过以下代码在表单加载时执行,

方法1:(xaml.cs)

ScrollViewer scrollViewer = GetVisualChild<ScrollViewer>(panelScroll);

if (scrollViewer != null)
   scrollViewer.ScrollToTop(); //Or ScrollToHome

方法2:用滚动查看器(ViewModel)包装RadPanelBar

    public class ScrollViewerBehavior
    {
        public static bool GetAutoScrollToTop(DependencyObject obj)
        {
            return (bool)obj.GetValue(AutoScrollToTopProperty);
        }

        public static void SetAutoScrollToTop(DependencyObject obj, bool value)
        {
            obj.SetValue(AutoScrollToTopProperty, value);
        }

        public static readonly DependencyProperty AutoScrollToTopProperty =
            DependencyProperty.RegisterAttached("AutoScrollToTop", typeof(bool), typeof(ScrollViewerBehavior), new PropertyMetadata(false, (o, e) =>
            {

                var scrollViewer = o as System.Windows.Controls.ScrollViewer;
                if (scrollViewer == null)
                {
                    return;
                }
                if ((bool)e.NewValue)
                {
                    scrollViewer.ScrollToTop();
                    SetAutoScrollToTop(o, false);
                }
            }));
    }


        public bool ResetScroll
        {
            get { return m_bResetScroll; }
            set
            {
                if (m_bResetScroll != value)
                {
                    m_bResetScroll = value;
                    OnPropertyChanged();
                }
            }
        }

<ScrollViewer Converter:ScrollViewerBehavior.AutoScrollToTop="{Binding ResetScroll, Mode=TwoWay}">

我仍然遇到同样的问题。无论如何,在表单加载期间我可以使滚动条显示在顶部吗?

0 个答案:

没有答案