WPF采用ItemsControl控件时添加滚动条

在使用ItemsControl控件显示集合时,当项增多时,会出现面板不够的情况,然而ItemControl控件本身没有实现滚动条,因此可以通过以下两种方法实现滚动条。

一、在ItemsControl模板中添加ScrollViewer

代码如下:

 <ItemsControl Height="365" ItemsSource="{Binding ...}" BorderThickness="0" Margin="0 5 0 5">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
              <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}" Template="{StaticResource NewScrollViewer}">
                   <ItemsPresenter/>
              ScrollViewer>
        ControlTemplate>
    ItemsControl.Template>
    <ItemsControl.ItemTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal" Margin="15 5 0 5">
                 <CheckBox Foreground="#3E3E42" Width="60" VerticalAlignment="Center" IsChecked="{Binding IsChecked}"/>
             StackPanel>
         DataTemplate>
   ItemsControl.ItemTemplate>
ItemsControl>

二、直接将ItemsControl的内容装进ScrollViewer控件

<StackPanel Grid.Row="1">
            <ScrollViewer VerticalScrollBarVisibility="Auto">
                <StackPanel>
                    <ItemsControl  ItemsSource="{Binding EquipmentCollection}" >
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical" >StackPanel>
                            ItemsPanelTemplate>
                        ItemsControl.ItemsPanel>
                    ItemsControl>
                StackPanel>
            ScrollViewer>
        StackPanel>

直接封装进ScrollViewer控件,发现无法实现滚动条,还需要在ScrollViewer控件内设定高度值,这样在内容超出范围后,滚动条就会出现。

  <ScrollViewer VerticalScrollBarVisibility="Auto" Height="350">

WPF采用ItemsControl控件时添加滚动条_第1张图片
ItemsControl的Items可能存在嵌套,此时若鼠标位于子控件中,滚动滚轮,滚动条无法进行响应,此时需要在子控件中绑定鼠标滚轮事件,并寻找父控件。

//在构造函数中绑定treeview的鼠标滚轮事件,原因是在xmal前台绑定时触发不了MouseWheel事件
public TreeViewList()
        {
            InitializeComponent();
            treeview.AddHandler(TreeView.MouseWheelEvent, new MouseWheelEventHandler(treeview_MouseWheel), true);
        }

//根据父控件对象查找指定类型的子控件
private T GetVisualChild<T>(DependencyObject parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

private void treeview_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            //在treeview的基础上,层层查找,直到找到它的父容器,是个NavToolbar(滚动条是NavToolbar的滚动条,并非treeview的)
            try
            {
                var scroll =
                GetVisualChild<ScrollViewer>(
                    ((((((((treeview.Parent as Grid).Parent as ChapterList).Parent as Grid).Parent as ScrollViewer).
                            Parent as Border).Parent as Grid).Parent as Grid).Parent as Border).Parent);
                if (scroll != null)
                {
                    scroll.ScrollToVerticalOffset(scroll.VerticalOffset - e.Delta);
                }
            }
            catch (Exception)
            {
            }
        }

参考:
[1]https://www.cnblogs.com/guyun/p/4252269.html.
[2]https://blog.csdn.net/qq_43024228/article/details/116710369

你可能感兴趣的:(wpf)