J4N
J4N

Reputation: 20717

How to disable scroll when I click on one child element

We have built a "menu", this menu is composed of several collection of items:

We have a ScrollViewer, which contains a control for each 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>

In this control, we have one title and a Listbox with all childs:

<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>

When I click on one DeviceMenuItem, it get selected properly, everything works fine. BUT, if I've scrolled a bit, hiding "Item A 1" and "Item A 2", and click on "Item A 3", the ScrollViewer scrolls to show the "Item A 1" and "Item A 2".

We don't want this, this scrolls too much. How to disable this scroll change?

Upvotes: 0

Views: 580

Answers (1)

zaphod-ii
zaphod-ii

Reputation: 524

When a partially visible element in a ScrollViewer is focused, the RequestBringIntoView event is raised, requesting that the parent ScrollViewer should scroll the whole element extent into view. However, often this is not the desired behavior as it causes this "jerking" scrolling. The easiest way to prevent is to handle the event before it bubbles to the parent ScrollViewer.

 <ScrollViewer>
        <ItemsControl x:Name="ic" RequestBringIntoView="OnItemsControlRequestBringIntoView">
            . . .
        </ItemsControl>
    </ScrollViewer>

private void OnListBoxRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
   e.Handled = true;
}

Upvotes: 2

Related Questions