none
Expanding GridViewRowPresenter in ListView

    Question

  • I'll warn you now, this might be hard to describe as I can't really use screenshots.  I have a ListViewItem style that can expand and collapse to show some detail text, based on this article (towards the bottom)...

    http://www.beacosta.com/blog/?p=20

    The code looks like this:

    <Style x:Key="myLVIStyle" TargetType="{x:Type ListViewItem}">  
          <Setter Property="Height" Value="Auto" /> 
          <Setter Property="Width" Value="Auto" /> 
          <Setter Property="Template">  
              <Setter.Value> 
                  <ControlTemplate TargetType="{x:Type ListViewItem}">  
                      <Border  
                        Name="Border" 
                        SnapsToDevicePixels="True" 
                        Background="{TemplateBinding Background}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        Padding="{TemplateBinding Padding}" 
                      > 
                          <StackPanel> 
                              <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>  
                              <TextBlock Text="{Binding Path=MyText}" Height="Auto" Width="Auto" Visibility="{Binding Path=ShowMyText, Converter={StaticResource BoolToVisConverter} }" Margin="2,0,0,4"/>  
                          </StackPanel> 
                      </Border> 
                      (...)  
    </Style> 

    This works fine for the most part, but I've run into a significant bug.  If the text in the TextBlock is longer (wider) than the content of the GridViewRowPresenter, then using the horizontal scrollbar will result in the content being moved, but the column headers stay in place.  So only the inside of my ListView scrolls horizontally, which results in the column headers and their content getting off sync.  That's bad.

    Anyone have any idea how I can solve this problem?  If the description doesn't make sense, I can throw together a mock up in paint or something.  Any help would be greatly appreciated.  Thanks!
    Monday, October 13, 2008 6:03 PM

All replies

  • Hi Thalandor46,

    You can set the Width property of  GridViewColumn to "Auto". Something like this should work(Put attention to the code in bold).

      <ListView IsSynchronizedWithCurrentItem="True" Margin="18,23,17,16" Name="listView1" > 
                <ListView.Resources> 
                    <Style  TargetType="{x:Type ListViewItem}"
                        <Setter Property="Height" Value="Auto" /> 
                        <Setter Property="Width" Value="Auto" /> 
                        <Setter Property="Template"
                            <Setter.Value> 
                                <ControlTemplate TargetType="{x:Type ListViewItem}"
                                    <Border BorderThickness="2"
                                        <StackPanel Background="Red"
                                            <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
                                            <TextBlock Text="{Binding Path=c2}" Height="Auto" Width="Auto"  Margin="2,0,0,4"/>   
                                        </StackPanel> 
                                    </Border> 
                                </ControlTemplate> 
                            </Setter.Value> 
                        </Setter> 
                    </Style> 
     
                </ListView.Resources> 
     
                <ListView.View> 
                    <GridView > 
                        <GridViewColumn Header="ID"  
                          DisplayMemberBinding="{Binding Path=c1}"  
                          Width="auto"/> 
                        <GridViewColumn Header="Name"  
                          DisplayMemberBinding="{Binding Path=c2}"  
                          Width="auto"/> 
                    </GridView> 
                </ListView.View> 
            </ListView> 


    Best Regards,
    Zhi-Xin



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by Zhi-Xin Ye Wednesday, October 15, 2008 9:39 AM typo
    Wednesday, October 15, 2008 9:37 AM