Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:43:45+00:00 2026-05-15T13:43:45+00:00

I have a ListView element with a DataTemplate for each ListViewItem defined as follows.

  • 0

I have a ListView element with a DataTemplate for each ListViewItem defined as follows. When run, the ListView’s height is not collapsed onto the items in the view, which is undesirable behavior:

<DataTemplate x:Key="LicenseItemTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"  />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="{Binding company}"></TextBlock>
        <Grid Grid.Row="1" Style="{StaticResource HiddenWhenNotSelectedStyle}">
            <Grid.RowDefinitions>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Button Grid.Row="0">ClickIt</Button>
        </Grid>
    </Grid>
</DataTemplate>

The second row of the outer grid has a style applied which looks like this. The purpose of the style is to expose a detail view of the selected data item :

<Style TargetType="{x:Type Grid}" x:Key="HiddenWhenNotSelectedStyle" >
    <Style.Triggers>
        <DataTrigger
            Binding="{Binding Path=IsSelected, 
                        RelativeSource={
                        RelativeSource 
                        Mode=FindAncestor, 
                        AncestorType={x:Type ListViewItem}
                        }
                        }" 
            Value="False">
            <Setter Property="Grid.Visibility" Value="Collapsed" />
        </DataTrigger>
        <DataTrigger
            Binding="{Binding Path=IsSelected, 
                        RelativeSource={
                        RelativeSource 
                        Mode=FindAncestor, 
                        AncestorType={x:Type ListViewItem}
                        }
                        }" 
            Value="True">
            <Setter
                Property="Grid.Visibility"
                Value="Visible"
            />
        </DataTrigger>
    </Style.Triggers>
</Style>

The ListView renders like this:
Height of ListView is twice what it should be.
(source: finitesolutions.com)

The desired appearance is this, when none of the elements are selected:
Height of ListView is collapsed to list items.
(source: finitesolutions.com)

…with, of course, the ListView’s height adjusting to accommodate the additional content when the second grid is made visible by selection. What can I do to get the desired behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:43:45+00:00Added an answer on May 15, 2026 at 1:43 pm

    While discussing the problem with WPF people at TechEd, I showed a Microsoft employee this question. He was nonplussed.

    We downloaded a tool which interrogates WPF layouts and identified the container as the “Virtualizing Stack Panel” element in the ListView.

    In a followup email, he wrote: “This is the fault of VirtualizingStackPanel. I’ve opened a bug about it. Hopefully it can be fixed in a future release. The workaround (using StackPanel) should be fine for now, as long as you don’t need the ListView to virtualize its content.

    The bug involves a step in VSP’s Measure algorithm that remembers the largest size ever discovered and forces all future Measure calls to report a size at least as large. In your case, the VSP is initially measured before any triggers have fired, so it computes the size as if everything were visible. When the triggers fire and collapse the buttons, the measure algorithm computes the correct (small) size, but then forces the result to be large again. The comment says something about avoiding unnecessary re-layouts while scrolling, but the code is running even when there’s no scrolling going on.”

    The work-around involves re-templating the ListView with this code:

    <ListView.ItemsPanel>
      <ItemsPanelTemplate>
        <StackPanel/>
      </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    

    This caused the list behavior to work as desired, but it carries the disadvantage of not having the memory management capabilities of the VirtualizingStackPanel. For my use, this was appropriate; the list items are never going to exceed 2000 or so at one time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Listview element: <ListView android:id=@+id/category_list android:layout_width=fill_parent android:layout_height=fill_parent android:layout_weight=1 android:background=@drawable/list_background android:layout_marginTop=10sp android:layout_marginLeft=10sp android:layout_marginRight=10sp
I have a problem with the UI virtualization of an ListView with hundreds of
I have one ListView which is showing me some data through an array (which
I have a WPF (.Net 4) ListView used to navigate a local Windows directory.
I want to make a form and put a divider between each form element,
Hi I have next XAML markup (just a piece of course): <Window.Resources> <GridView x:Key=gridview>
I'm binding a ListView to a collection of objects which is working fine. Unfortunately
I am wondering how can I insert an element at the beginning of the
I'm having some issues when trying to bind a property of a UserControl in
I looked at several related answers, and determined that I can programmatically clear the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.