将第一个ListBoxItem的高度设置为WP7中其他项的差异

时间:2011-04-10 14:41:21

标签: c# silverlight windows-phone-7 listbox items

我需要在WP7的底部显示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");                       

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

EDIT ::

好的,这应该有效:

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

1 个答案:

答案 0 :(得分:3)

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

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

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

谢谢, Stefan Wick - Microsoft Silverlight