单击一个 child 元素时如何禁用滚动
How to disable scroll when I click on one child element
我们建了一个"menu",这个菜单由几个collection项组成:
- A 类项目
- 项目 A 1
- 项目 A 2
- 项目 A 3
- B 类项目
- 项目 B 1
- 项目 B 2
- 项目 B 3
- 项目 B 4
- 项目 B 5
- C 类项目
- 项目 C 1
- 项目 C 2
我们有一个 ScrollViewer,其中包含每个 collection:
的控件
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="False" >
<ItemsControl ItemsSource="{Binding Collections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MenuCollections SlotCollection="{Binding}" SelectedItem="{Binding Path=DataContext.SelectedItem, RelativeSource={RelativeSource AncestorType=ItemsControl}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
在此控件中,我们有一个标题和一个包含所有子项的列表框:
<StackPanel Name="RootContainer" Orientation="Vertical">
<StackPanel.DataContext>
<local:DeviceMenuSlotCollectionViewModel/>
</StackPanel.DataContext>
<Label Content="{Binding SlotCollection.Name}" Margin="5,7,0,10"/>
<ListBox ItemsSource="{Binding Path=CollectionView, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Stretch" Padding="0" BorderThickness="0" SelectedValue="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type local:DeviceMenuSlotCollection}}, Mode=TwoWay}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:DeviceMenuItem Slot="{Binding}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=SelectedItem}" Margin="0"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
当我点击一个 DeviceMenuItem 时,它被正确选中,一切正常。但是,如果我滚动了一下,隐藏 "Item A 1" 和 "Item A 2",然后单击 "Item A 3",ScrollViewer 滚动显示 "Item A 1" 和 "Item A 2"。
我们不想要这个,它滚动得太多了。如何禁用此滚动更改?
当 ScrollViewer
中的部分可见元素获得焦点时,将引发 RequestBringIntoView 事件,请求父 ScrollViewer
应将整个元素范围滚动到视图中。但是,这通常不是我们想要的行为,因为它会导致 "jerking" 滚动。最简单的预防方法是在事件冒泡到父级之前处理该事件 ScrollViewer
.
<ScrollViewer>
<ItemsControl x:Name="ic" RequestBringIntoView="OnItemsControlRequestBringIntoView">
. . .
</ItemsControl>
</ScrollViewer>
private void OnListBoxRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
我们建了一个"menu",这个菜单由几个collection项组成:
- A 类项目
- 项目 A 1
- 项目 A 2
- 项目 A 3
- B 类项目
- 项目 B 1
- 项目 B 2
- 项目 B 3
- 项目 B 4
- 项目 B 5
- C 类项目
- 项目 C 1
- 项目 C 2
我们有一个 ScrollViewer,其中包含每个 collection:
的控件<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="False" >
<ItemsControl ItemsSource="{Binding Collections}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:MenuCollections SlotCollection="{Binding}" SelectedItem="{Binding Path=DataContext.SelectedItem, RelativeSource={RelativeSource AncestorType=ItemsControl}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
在此控件中,我们有一个标题和一个包含所有子项的列表框:
<StackPanel Name="RootContainer" Orientation="Vertical">
<StackPanel.DataContext>
<local:DeviceMenuSlotCollectionViewModel/>
</StackPanel.DataContext>
<Label Content="{Binding SlotCollection.Name}" Margin="5,7,0,10"/>
<ListBox ItemsSource="{Binding Path=CollectionView, UpdateSourceTrigger=PropertyChanged}" HorizontalContentAlignment="Stretch" Padding="0" BorderThickness="0" SelectedValue="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type local:DeviceMenuSlotCollection}}, Mode=TwoWay}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:DeviceMenuItem Slot="{Binding}" SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=SelectedItem}" Margin="0"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</StackPanel>
当我点击一个 DeviceMenuItem 时,它被正确选中,一切正常。但是,如果我滚动了一下,隐藏 "Item A 1" 和 "Item A 2",然后单击 "Item A 3",ScrollViewer 滚动显示 "Item A 1" 和 "Item A 2"。
我们不想要这个,它滚动得太多了。如何禁用此滚动更改?
当 ScrollViewer
中的部分可见元素获得焦点时,将引发 RequestBringIntoView 事件,请求父 ScrollViewer
应将整个元素范围滚动到视图中。但是,这通常不是我们想要的行为,因为它会导致 "jerking" 滚动。最简单的预防方法是在事件冒泡到父级之前处理该事件 ScrollViewer
.
<ScrollViewer>
<ItemsControl x:Name="ic" RequestBringIntoView="OnItemsControlRequestBringIntoView">
. . .
</ItemsControl>
</ScrollViewer>
private void OnListBoxRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}