2

我需要在 WP7 的底部显示 ListBox 中的项目列表。因此,如果我有一些项目的高度总和小于 ListBox 高度,我需要在顶部有一个空白项目,其高度不同。

我必须这样做,因为我设置了 Listbox 的 ItemSource,所以在加载它们之前我不知道所有项目的正确高度是多少。

在每个项目的 Item_loaded 事件中,我保存高度,最后我需要设置第一个的高度。

<ListBox x:Name="ConvListBox" Margin="0,0,-12,0" >
            <ListBox.ItemTemplate  >
                <DataTemplate >
                    <Grid>
                        <StackPanel Name="BaloonMessage" Margin="3,0,0,0" Loaded="Baloon_Loaded" Tag="{Binding IsSentMsg}" >
                        <TextBlock Name="SMSText" Text="{Binding SMSText}" Margin="7,3,8,35" TextWrapping="Wrap" Height="Auto" Width="Auto" FontSize="22" Foreground="White"/>
                        </StackPanel>
                   </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我设置 ItemsSource 并在顶部添加一个空白项,在底部添加一个空白:

            ObservableCollection<ClassMessaggio> messaggi =
                new ConversazioneViewModel(MessaggioConversazione).Conversazione;

            ClassMessaggio FirstLineScrollMessage = new ClassMessaggio();
                            FirstLineScrollMessage.IsSentMsg = "3";
                            messaggi.Insert(0, FirstLineScrollMessage); 

            ClassMessaggio LastLineScrollMessage = new ClassMessaggio();
                            LastLineScrollMessage.IsSentMsg = "2";
                            messaggi.Insert(messaggi.Count, LastLineScrollMessage);

            this.ConvListBox.ItemsSource = messaggi;

在 Item_Loaded 我正在尝试这个:

var Panel = (StackPanel) sender;
        if (Panel != null)
        {
            Grid grid = (Grid)Panel.Parent;

            Border baloon = (Border)Panel.FindName("Baloon");
            baloon.Width = grid.Width - 100;

            if (Panel.Tag.ToString() == "3")
            {
                TotalBaloonsHeight = 0;

                baloon.Background = grid.Background;
                baloon.Name = "FirstScrollBaloon";
            }
            else if (Panel.Tag.ToString() == "2")
            {
                baloon.Height = 2;
                Panel.Height = 2;
                grid.Height = 2;

                Border FirstBaloon = (Border)ConvListBox.FindName("FirstScrollBaloon");                       
                if (FirstBaloon != null)
                {
                    FirstBaloon.Height =  ConvListBox.Height - TotalBaloonsHeight;
                }

            }
            else
            {
            TotalBaloonsHeight = TotalBaloonsHeight + baloon.Height;

            }

        }

我的问题是这条线总是返回 null :(

  Border FirstBaloon = (Border)ConvListBox.FindName("FirstScrollBaloon");                       

我希望很清楚,对不起我的英语。

编辑::

好的,这应该工作:

var Baloons = LayoutRoot.GetVisualDescendants().OfType<Border>();
                   foreach (var FirstBaloon in Baloons)
                    {
                        if (FirstBaloon != null)
                        {
                            if (FirstBaloon.Name == "FirstScrollBaloon")
                            {
                                FirstBaloon.Height = ConvListBox.ActualHeight - TotalBaloonsHeight;
                                break;
                            }
                        }
                    }        
4

1 回答 1

3

您可以使用以下代码访问第一个 ListBoxItem:

ListBoxItem item0 = ConvListBox.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;

从那里你可以修改它的高度等。

谢谢,Stefan Wick - Microsoft Silverlight

于 2011-04-10T16:04:28.793 回答