Как удалить свернутое пространство в списке с элементами типа wrappanel, включая полный исходный код + изображение

У меня есть ListBox с ItemsPanelTemplate из WrapPanel. Есть некоторые левые и правые отступы свернутых элементов, от которых я хочу избавиться... Когда элементы видны, изображение представляет собой красивый квадрат с пятью кнопками в строке. Вот изображение: http://iterationx.posterous.com/71739342

Как я могу избавиться от заполнения в начале первой кнопки, когда мои кнопки AE свернуты?

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight"
        >
        <Grid Margin="100,100,100,100">
        <StackPanel>
            <ListBox Name="MainButtonListBox" HorizontalContentAlignment="Stretch" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Padding="0" Width="550">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate >
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate >
                    <DataTemplate>
                        <Button  Content="{Binding Path=ButtonName}"  Click="Button_Click" DataContext="{Binding}" Width="100"  Height="75"   Visibility="{Binding Path=Visibility}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class ButtonObj
        {
            public string ButtonName { get; set; }
            public Visibility Visibility { get; set; }
        }

        List<ButtonObj> AEList = new List<ButtonObj>();
        List<ButtonObj> MainButtonList = new List<ButtonObj>();
        public MainWindow()
        {
            InitializeComponent();

            AEList.Add(new ButtonObj() { ButtonName = "A", Visibility = Visibility.Collapsed });
            AEList.Add(new ButtonObj() { ButtonName = "B", Visibility = Visibility.Collapsed });
            AEList.Add(new ButtonObj() { ButtonName = "C", Visibility = Visibility.Collapsed });
            AEList.Add(new ButtonObj() { ButtonName = "D", Visibility = Visibility.Collapsed });
            AEList.Add(new ButtonObj() { ButtonName = "E", Visibility = Visibility.Collapsed });

            foreach (ButtonObj b1 in AEList) MainButtonList.Add(b1);

            MainButtonList.Add(new ButtonObj() { ButtonName = "Expand A-E", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "G", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "H", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "I", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "Collapse A-E", Visibility = Visibility.Visible });

            MainButtonList.Add(new ButtonObj() { ButtonName = "K", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "L", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "M", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "N", Visibility = Visibility.Visible });
            MainButtonList.Add(new ButtonObj() { ButtonName = "O", Visibility = Visibility.Visible });

            MainButtonListBox.ItemsSource = MainButtonList;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            ButtonObj obj = btn.DataContext as ButtonObj;

            if (obj.ButtonName == "Expand A-E")
            {
                foreach (ButtonObj b1 in AEList) b1.Visibility = System.Windows.Visibility.Visible; 
            }
            if (obj.ButtonName == "Collapse A-E")
            {
                foreach (ButtonObj b1 in AEList) b1.Visibility = System.Windows.Visibility.Collapsed; 
            }

            MainButtonListBox.ItemsSource = null;
            MainButtonListBox.ItemsSource = MainButtonList;
        }
    }
}

person patrick    schedule 22.09.2011    source источник


Ответы (1)


вы просто сворачиваете содержимое элемента списка. Вы должны стилизовать ListBoxItem (который не удаляется).

<ListBox Name="MainButtonListBox" HorizontalContentAlignment="Stretch" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Padding="0" Width="550" >
     <ListBox.ItemContainerStyle>
          <Style TargetType="{x:Type ListBoxItem}">
              <Setter Property="Padding" Value="0,0,0,0"/>
          </Style>
     </ListBox.ItemContainerStyle>
     <ListBox.ItemsPanel>
          <ItemsPanelTemplate >
               <WrapPanel />
           </ItemsPanelTemplate>
     ...

как правило, было бы лучше использовать возможности фильтра, чтобы элементы действительно удалялись из списка, а не только скрытое содержимое (элементы по-прежнему можно выбирать с помощью клавиатуры)

вы также можете (лучше, чем в первом стиле) свернуть весь элемент:

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">                        
        <Setter Property="Visibility" Value="{Binding Path=Visibility}"/>
    </Style>
</ListBox.ItemContainerStyle>
person jnkb    schedule 23.09.2011