禁用滚动时如何保留滚动位置

发布于 2024-11-28 03:38:54 字数 4127 浏览 0 评论 0原文

在我的 win Phone 7 应用程序中,我想禁用列表框中的垂直滚动。但是当我使用时

listbox.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);

列表框滚动到顶部。如何在禁用滚动的情况下保留滚动位置?

编辑:我正在考虑在列表框处理事件之前通过吞咽事件来禁用滚动。但是当我尝试处理 ManipulationStarted 和 ManipulationCompleted 时,我有例外。我应该处理哪些事件才能使列表框无法滚动? 当我将 ManipulationStarted、ManipulationDelta 和 ManipulationCompleted 标记为在代码中处理时,此堆栈跟踪出现 Null 异常:

at Microsoft.Phone.Gestures.GestureHelper.ReleaseMouseCaptureAtGestureOrigin()
        at Microsoft.Phone.Gestures.GestureHelper.NotifyMove(InputDeltaArgs args)
        at Microsoft.Phone.Gestures.ManipulationGestureHelper.Target_ManipulationDelta(Object sender, ManipulationDeltaEventArgs e)
        at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
        at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
        at Microsoft.Xna.Framework.Input.UnsafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
        at Microsoft.Xna.Framework.Input.SafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
        at Microsoft.Xna.Framework.Input.WindowMessageHooker.Hook.WndProc(IntPtr msgWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

编辑:我发现默认列表框在禁用滚动时可以按我想要的方式工作。但我的列表框有一个自定义模板,其中包含一个包含 ItemPresenter 的堆栈以禁用 UI 虚拟化。在这种情况下,当禁用滚动时,列表框会自动滚动到顶部。

编辑:这里是列表框的模板:

<phone:PhoneApplicationPage.Resources>
        <Style x:Key="ListBoxStyle1" TargetType="ListBox">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
                            <StackPanel Orientation="Vertical" Width="468">
                                <ItemsPresenter d:LayoutOverrides="Width"/>
                            </StackPanel>
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>

和xaml:

<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged" Style="{StaticResource ListBoxStyle1}" GotFocus="gotfocus" LostFocus="lostfocus">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432">
                          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                          <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

在gotfocus和lostfocus函数中,我分别禁用和启用滚动。该列表框来自默认的数据绑定应用程序。当我向下滚动并单击一项时,列表框会滚动到顶部。当我不使用堆栈来保存 ItemPresenter 时,不会发生这种情况。

In my win phone 7 app I want to disable vertical scrolling in a listbox. But when I use

listbox.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);

The listbox scrolls to the top. How can I retain the scroll position while have scrolling disabled?

Edit: I'm thinking of disabling scrolling by swallowing events before listbox handle them. But when I tried to handle ManipulationStarted and ManipulationCompleted, I have exception. What events I should handle to make listbox cannot scroll?
When I mark ManipulationStarted, ManipulationDelta and ManipulationCompleted as handled in my code, I have Null Exception with this stack trace:

at Microsoft.Phone.Gestures.GestureHelper.ReleaseMouseCaptureAtGestureOrigin()
        at Microsoft.Phone.Gestures.GestureHelper.NotifyMove(InputDeltaArgs args)
        at Microsoft.Phone.Gestures.ManipulationGestureHelper.Target_ManipulationDelta(Object sender, ManipulationDeltaEventArgs e)
        at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
        at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)
        at Microsoft.Xna.Framework.Input.UnsafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
        at Microsoft.Xna.Framework.Input.SafeNativeMethods.CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
        at Microsoft.Xna.Framework.Input.WindowMessageHooker.Hook.WndProc(IntPtr msgWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)

Edit: I have found that default listbox works as I want when disable scrolling. But my listbox has a custom template with a stack holding ItemPresenter to disable UI virtualization. In that scenario when disable scrolling, the listbox auto scrolls to the top.

Edit: here is the listbox's template:

<phone:PhoneApplicationPage.Resources>
        <Style x:Key="ListBoxStyle1" TargetType="ListBox">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Transparent"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBox">
                        <ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}">
                            <StackPanel Orientation="Vertical" Width="468">
                                <ItemsPresenter d:LayoutOverrides="Width"/>
                            </StackPanel>
                        </ScrollViewer>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </phone:PhoneApplicationPage.Resources>

and xaml:

<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged" Style="{StaticResource ListBoxStyle1}" GotFocus="gotfocus" LostFocus="lostfocus">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                      <StackPanel Margin="0,0,0,17" Width="432">
                          <TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                          <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                      </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

In gotfocus and lostfocus function, I disable and enable scrolling respectively. This listbox is from the default databound application. When I scroll down and click one item, the listbox scrolls to the top. It doesn't happen when I don't use a stack to hold ItemPresenter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷途知返 2024-12-05 03:38:54

你可以这样做:

ScrollViewer.SetVerticalScrollBarVisibility(listbox, ScrollBarVisibility.Disabled);

You can do this:

ScrollViewer.SetVerticalScrollBarVisibility(listbox, ScrollBarVisibility.Disabled);
风筝在阴天搁浅。 2024-12-05 03:38:54

您不能将属性 IsHitTestVisible 设置为 false 以禁用滚动吗?

Can't you otherwise set the property IsHitTestVisible to false to disable the scrolling?

黄昏下泛黄的笔记 2024-12-05 03:38:54

我正在处理一个小项目,也有一个自定义模板列表框。我测试了以下代码,这似乎对我来说效果很好:

ScrollViewer.SetVerticalScrollBarVisibility(YourListBox, ScrollBarVisibility.Disabled);
YourListBox.ScrollIntoView(YourListBox.SelectedItem);

I have a small project I'm working on, also with a custom templated listbox. I tested the following code and that seemed to work fine for me:

ScrollViewer.SetVerticalScrollBarVisibility(YourListBox, ScrollBarVisibility.Disabled);
YourListBox.ScrollIntoView(YourListBox.SelectedItem);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文