Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identical ListViews display differently - Columns don't stretch on one

I have run into a bizarre problem. I am displaying a UserControl within a UserControl and at the moment each contains a completely identical ListView, as in I copied and pasted it from one to the other. On the parent UserControl, the ListView displays properly, but on the sub-UserControl (whatever it's called), the ListView columns refuse to stretch to fit their content. I have no idea why this is happening since the code of both ListViews is exactly the same. Both are placed directly on the grid.

enter image description here

On the top left is the ListView from the parent control and on the bottom right is that of the child control. Here is the code for both ListViews:

<ListView ItemsSource="{Binding Adventurers}"
              Name="AdvListView"
              HorizontalAlignment="Stretch"
              Grid.Column="2"
              Grid.ColumnSpan="1"
              Grid.Row="2"
              ScrollViewer.CanContentScroll="False"
              BorderThickness="3">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <cmd:EventToCommand Command="{Binding ShowAdvWindowCommand}"
                                    CommandParameter="{Binding ElementName=AdvListView, Path=SelectedItem}"
                                    PassEventArgsToCommand="False" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="VerticalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.Resources>
            <DataTemplate x:Key="NameTemplate">
                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Name}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
            </DataTemplate>
            <DataTemplate x:Key="StatusTemplate">
                <TextBlock Margin="2,1,1,1" Text="{Binding Status}" VerticalAlignment="Center" />
            </DataTemplate>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" CellTemplate="{StaticResource NameTemplate}" />
                <GridViewColumn Header="Status" CellTemplate="{StaticResource StatusTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>

Why would this work on one and not the other? I'm really at a loss at this point.

Update: In the parent UserControl, the grid layout is:

<Grid Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
        //Listview here among other controls
</Grid>

In the child:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
        //Listview here
</Grid>

Modifying Grid.Row, Grid.Column, RowSpan, or ColumnSpan to be correct doesn't fix the problem.

Update: A bit more info that may be relevant: When I try to use Snoop on my program. I get this error:

"BindingFailure was detected - The assembly with display name 'Snoop.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Snoop.XmlSerializers, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."

Snoop has no problem with other programs. I don't know if this is related to the issue at all, but I figured I'd post it anyways.

Update: I don't think this would matter, but just to be thorough, here is where the child UserControl is created within the parent:

<Grid>
    //Other controls
    <UserControl Grid.Column="3"
                Grid.Row="3"
                Visibility="{Binding AdventurerInfoVisibility}">
        <view:AdventurerInfoView />
    </UserControl>
</Grid>

At this point I'm completely stumped. I can create a copy of the ListView in the parent UserControl and have it display properly... but for some reason it doesn't want to behave in the child UserControl... I'm open to any ideas and if you need more code I would be more than happy to provide it.

like image 414
Jason D Avatar asked Jun 12 '13 23:06

Jason D


1 Answers

After much tinkering and frustration, I found out what was causing the problem. The child UserControl's Visibility was set to Collapsed by default and was made visible by a button click. When I made it visible by default, the columns stretched properly.

Now I just need to find out how to allow it to start collapsed and still maintain the stretched column width.

like image 173
Jason D Avatar answered Oct 21 '22 16:10

Jason D