如何在C#WPF应用程序中拖动listviewitem时滚动listview? [英] How to scroll listview on dragging of listviewitem in C# WPF App?

查看:77
本文介绍了如何在C#WPF应用程序中拖动listviewitem时滚动listview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <ScrollViewer Template="{StaticResource ScrollViewerControlTemplate}" >
            <ListView x:Name="AllFunctionView" Background="#1b1b1b" Margin="15,0,15,0" PreviewMouseDown="AllFunctionView_PreviewMouseDown" PreviewMouseMove="AllFunctionView_PreviewMouseMove" PreviewMouseUp="AllFunctionView_PreviewMouseUp">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <DataTemplate.Resources>
                            <Style TargetType="{x:Type StackPanel}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding SubElement}" Value="NoElement">
                                        <Setter Property="Visibility" Value="Collapsed"></Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </DataTemplate.Resources>
                        <Border>
                            <StackPanel Orientation="Horizontal" Background="#80d3d3d3" Height="30" Width="100" PreviewMouseDown="TextBlock_PreviewMouseDown">
                                <Image Width="20" Height="20" Source="{Binding ImageIcon}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,0,0,0"/>
                                <TextBlock Margin="5,0,0,0" Text="{Binding SubElement}" TextAlignment="Center" VerticalAlignment="Center"  FontSize="12" Foreground="White"></TextBlock>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>

                <ListView.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate>
                                            <Expander IsExpanded="True" Style="{StaticResource ExpanderStyle1}">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <!--<Image Height="20" Width="20" Source="{Binding Name, Converter={StaticResource ImageConverter}}" HorizontalAlignment="Left"/>-->
                                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" VerticalAlignment="Bottom" />
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </ListView.GroupStyle>
            </ListView>
        </ScrollViewer>

我在scrollviewer里面有listview.

I have listview inside scrollviewer.

要在拖动listviewitem时滚动listview.

Want to scroll listview on dragging of listviewitem.

如何?

推荐答案

要在拖动时实现ListView自动滚动,我们可以使用ListView的事件DragOver,创建一个函数来获取 ListView的滚动查看器,然后调整位置.

To implement ListView auto scroll while dragging, we can use the event DragOver of the ListView, create a function to get the scrollviewer of the ListView and after that just adjust the position.

public static childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
            // Search immediate children first (breadth-first)
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is childItem)
                    return (childItem)child;

                else
                {
                    childItem childOfChild = FindVisualChild<childItem>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }
            return null;
}

private void AllFunctionView_DragOver(object sender, DragEventArgs e)
{
            ListView li = sender as ListView;
            ScrollViewer sv = FindVisualChild<ScrollViewer>(AllFunctionView);

            double tolerance = 10;
            double verticalPos = e.GetPosition(li).Y;
            double offset = 3;

            if (verticalPos < tolerance) // Top of visible list?
            {
                sv.ScrollToVerticalOffset(sv.VerticalOffset - offset); //Scroll up.
            }
            else if (verticalPos > li.ActualHeight - tolerance) //Bottom of visible list?
            {
                sv.ScrollToVerticalOffset(sv.VerticalOffset + offset); //Scroll down.    
            }
}

截屏:


这篇关于如何在C#WPF应用程序中拖动listviewitem时滚动listview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆