views:

22

answers:

1

Hi folks,

from this question, I drilled down the problem to a listbox, that doesn't resize, when the Listbox-Items shrink. It resizes accordingly, when the size of the items grow, but it doesn't shrink, when the size of the items decrease.

The items can grow/shrink because the items containing textboxes, that resize with the input.

Jeremiah suggested to start a new question with more code to show, so here we go:

Our evil listbox is part of a UserControl, that contains a StackPanel with a Label (HorizontalAlignment=Center), the listbox (HA=Left) and a Button (HA=Right). The listbox-items are datalinked to an ObservableCollection

You will recognize beautiful BackgroundColors on the ListBox and the ListBoxItems. I used them to be able to tell wheter the Items or the Listbox itself doesn't shrink. I found out, that the Items shrink, but the Listbox doesn't.

Ok, here is the code of my UserControl:

<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
  <StackPanel.Background>
    <SolidColorBrush Color="{StaticResource ColorBasicDark}"/>
  </StackPanel.Background>

  <sdk:Label x:Name="LabelServiceName" FontSize="{StaticResource FontSizeMedium}" Margin="2" HorizontalAlignment="Center" Content="LabelServiceName">
    <sdk:Label.Foreground>
      <SolidColorBrush Color="{StaticResource ColorBasicLight}"/>
    </sdk:Label.Foreground>
  </sdk:Label>

  <ListBox x:Name="ListBoxCharacteristics" BorderBrush="{x:Null}" Margin="0" HorizontalContentAlignment="Left" FontSize="9.333" HorizontalAlignment="Left">
    <ListBox.Foreground>
      <SolidColorBrush Color="{StaticResource ColorBasicLight}"/>
    </ListBox.Foreground>

    <!-- DataTemplate to display the content -->
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel x:Name="StackPanelBorder" Orientation="Horizontal" HorizontalAlignment="Left">
          <TextBox x:Name="TextBoxCharacteristicName" Style="{StaticResource InputTextBox}" Text="{Binding Name}" />
          <TextBox x:Name="TextBoxSep" Style="{StaticResource ReadOnlyTextBox}" Text="=" />
          <TextBox x:Name="TextBoxFuncOrValue" Style="{StaticResource InputTextBox}" Text="{Binding Value.Text}" />
          <TextBox x:Name="TextBoxValue" Style="{StaticResource ReadOnlyTextBox}" />
          <Button x:Name="ButtonRemove" Style="{StaticResource BasicButtonStyle}" Content="-" Click="ButtonRemove_Click" />
        </StackPanel>
      </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalAlignment" Value="Left" />
        <Setter Property="Background" Value="Yellow" />
      </Style>
    </ListBox.ItemContainerStyle>

    <ListBox.Background>
      <SolidColorBrush Color="Red" />
    </ListBox.Background>
  </ListBox>

  <Button x:Name="ButtonAddCharaDisplayObject" Style="{StaticResource BasicButtonStyle}" Content="+" HorizontalAlignment="Right" Click="ButtonAddCharaDisplayObject_Click" />
</StackPanel>

I have no idea why the listbox doesn't shrink when the size of the items shrink, although I have set the listbox' size to Auto and HorizontalAlignment to Left

Thanks in advance, Frank

A: 

Well... I don't have all of your code. But, I simplified what you had above to this and it works.

I hope this will help you, in some way, figure out your problem. Once again, it could be the parent of this control causing the problems. It could also be one of your styles you are applying. Try stripping out EVERYTHING from your control that doesn't have to be there, then add it back slowly to find the culprit.

I created a new silverlight application, and this is literally the only thing in it. The listbox grows and shrinks as expected.

XAML:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"
    x:Class="Test.MainPage">

    <Grid x:Name="LayoutRoot">
        <StackPanel VerticalAlignment="Top" HorizontalAlignment="Left"> 
          <StackPanel.Background> 
            <SolidColorBrush Color="Black"/> 
          </StackPanel.Background> 

          <ListBox x:Name="ListBox" BorderBrush="{x:Null}" Margin="0" HorizontalContentAlignment="Left" FontSize="9.333" HorizontalAlignment="Left"> 
            <ListBox.Foreground> 
              <SolidColorBrush Color="Silver"/> 
            </ListBox.Foreground> 

            <!-- DataTemplate to display the content --> 
            <ListBox.ItemTemplate> 
              <DataTemplate> 
                <StackPanelOrientation="Horizontal" HorizontalAlignment="Left"> 
                  <TextBox FontSize="30" Text="{Binding}" />      
                </StackPanel> 
              </DataTemplate> 
            </ListBox.ItemTemplate> 

            <ListBox.ItemContainerStyle> 
              <Style TargetType="ListBoxItem"> 
                <Setter Property="HorizontalAlignment" Value="Left" /> 
                <Setter Property="Background" Value="Yellow" /> 
              </Style> 
            </ListBox.ItemContainerStyle> 

            <ListBox.Background> 
              <SolidColorBrush Color="Red" /> 
            </ListBox.Background>   

          </ListBox> 

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Height="30">
                <Button Content="Add" Click="Add_Click" Width="100"/>
                <Button Content="Remove" Click="Remove_Click"  Width="100"/> 
            </StackPanel>  
        </StackPanel>       
    </Grid>
</UserControl>

Code Behind:

using System;
using System.Windows;
using System.Windows.Controls;

namespace Test
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();

            Count = 8;
        }

        private int Count;

        private void Add_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Count = Count * 8;

            ListBox.Items.Add("Hi Mom (" + Count.ToString() + ")");
        }

        private void Remove_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            ListBox.Items.RemoveAt(ListBox.Items.Count-1);
        }
    }
}
Jeremiah
I didn't solved the problem yet, but as it is not a top priority, it is on hold till other functionality is implemented. One thing I thought might have caused this was, that the parent of the UC is a canvas, but when placed it in a Grid, the problem still stays the same. If I get it done somehow, I will report back.
Aaginor