ListBox Scrollviewer Weird behavior

I have ScrollViewer in the ControlTemplate of my ListBox and I am using .Net 4.0 I am facing two issues on different values of CanContentScroll.

  1. when CanContentScroll is True.

    In this case, when I scroll through ListBox with a mouse, ScrollViewer seems to skip 1 or 2 items randomly out of the view. But SelectedItem's selection works properly.

  2. when CanContentScroll is False.

    In this case, scrolling with mouse works fine. but now SelectedItem's selection does not select the actual SelectedItem, but selects the item above or below of it. or continues to display previously SelectedItem.

please help me out.

<Style TargetType="ListBox" x:Key="ListBoxStyle">
        <Setter Property="ItemContainerStyle" Value="{StaticResource ItemContainerStyle}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <ScrollViewer CanContentScroll="True">
                        <ItemsPresenter/>
                    </ScrollViewer>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ItemContainerSTyle:

<Style x:Key="ItemContainerStyle" BasedOn="{StaticResource listBoxItemStyle}" TargetType="ListBoxItem">
        <Setter Property="helper:FluidSelectionAttachedPropertyHelper.IgnoreRightClick" Value="True"/>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger >
        </Style.Triggers>
    </Style>

ItemTemplate:

<Style TargetType="ListBoxItem" x:Key="listBoxItemStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter Margin="2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Answers


The weird behaviour that you describe is exactly what you asked for using your bizarre ItemContainerStyle:

<Style x:Key="ItemContainerStyle" BasedOn="{StaticResource listBoxItemStyle}" 
    TargetType="ListBoxItem">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger >
    </Style.Triggers>
</Style>

Here, you set the items to disappear when they are selected.

For your other, scrolling problem, I would advise you to read the ScrollViewer.CanContentScroll Property page on MSDN as it seems to be working just fine in my opinion:

The default behavior of the ScrollViewer is to use physical units to scroll its content. However, in cases where the CanContentScroll is set to true, the content could use logical units to scroll.

It should be noted that it can be difficult to spot the difference between these two settings if the items are just string based.


Need Your Help

HADOOP - Problems copying text files into HDFS

hadoop mapreduce hdfs word-count

I am implementing Hadoop single-node-cluster following the prominent Michael Noll Tutorial. The cluster is working, checking with jps shows that all components are running after execution of

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.