如何为我的WPF ListView安装类似滚动条的android。我的意思是说,滚动条只有在用户滚动时才可见。一旦用户停止滚动,它们应该淡出。怎么做到这一点?
答案 0 :(得分:-1)
您可以使用DispatcherTimer
命名空间中的System.Windows.Threading
类
这是您窗口的XAML标记:
<Window x:Class="testwpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListView Name="list" ScrollViewer.VerticalScrollBarVisibility="Hidden" Loaded="list_Loaded">
</ListView>
</Window>
这是背后的代码:
public partial class MainWindow : Window
{
DispatcherTimer dispatcherTimer;
public MainWindow()
{
InitializeComponent();
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
// we are here only if for the last 500 ms there were no changes of
// the scrollbar's vertical offset and we can hide a scrollbar
list.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
dispatcherTimer.Stop();
}
private void list_Loaded(object sender, RoutedEventArgs e)
{
// attach a handler
Decorator border = VisualTreeHelper.GetChild(list, 0) as Decorator;
ScrollViewer scroll = border.Child as ScrollViewer;
scroll.ScrollChanged += list_ScrollChanged;
}
private void list_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
// when vertical scroll position is changed, we set VerticalScrollBarVisibilityProperty
// to Auto (scroll bar is displayed only if needed) and run a timer with 500 ms interval
if (e.VerticalChange != 0)
{
list.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Auto);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
dispatcherTimer.Start();
}
}
}
这是一个原始的例子。当然,您可以应用自己的条件来定义是否应隐藏滚动条。例如,如果仍然在滚动条上按下鼠标左键,则可能决定不隐藏滚动条。