Silverlight 4: Listbox does not compress when its items are compressed

from this question , I unfolded the problem to a list that does not change when the Listbox-Items are reduced. It resizes accordingly when the size of objects grows, but it does not decrease when the size of objects decreases.

Elements can grow / shrink because elements contain text fields that are resized using input.

Jeremiah suggested starting a new question with a lot of code to show, so we go:

Our wicked list is part of a UserControl that contains a StackPanel with a label (HorizontalAlignment = Center), a list (HA = left), and a button (HA = Right). Listbox items bound to ObservableCollection

You will recognize beautiful BackgroundColors in ListBox and ListBoxItems. I used them to say that the Elements or the list itself are not abbreviated. I found out that items are being compressed, but Listbox does not.

So 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 list does not decrease when the size of the elements decreases, although I set the list size to Auto and HorizontalAlignment to Left

Thanks in advance, Frank

+5
source share
2 answers

, . , Silverlight 3 on, ListBox VirtualizationStackPanel ListItems. StackPanel, VirtualizationStackPanel , , . , , ListBox , , ListBox ( ) - - , VirtualizationStackPanel .

, ListBox StackPanel VirtualizationStackPanel. , !

<ListBox HorizontalContentAlignment="Left" FontSize="9.333" HorizontalAlignment="Left"> 

    ... // other listbox related stuff

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

</ListBox> 
+5

... . , , .

, - . , , . , . , , , .

silverlight, . , .

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>

:

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);
        }
    }
}
0

All Articles