Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 with other items before
</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.

share|improve this question
If they are identical can you post the xaml of the grid control that contains the listviews? – Kazuo 12 hours ago
I've updated my post. – Jason D 11 hours ago

1 Answer

I create a deom use your code, and it does not to repeat your problem.

I think maybe your child usercontrol had some TextBlock style, and override your container setting.

share|improve this answer
maybe you can use Snoop to watch column width and set from what way. – muzizongheng 2 hours ago
Well this certainly is interesting. When I try to use Snoop on my program, this happens: "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 works just fine on other programs, though. – Jason D 52 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.