开发者

Set Height of First ListBoxItem as difference of the other Items in WP7

开发者 https://www.devze.com 2023-02-23 17:04 出处:网络
i need to show a list of item in a ListBox from the bottom in WP7. So in case i have some items that the height sum of them is < of ListBox Height i need to have a blank item at the top with the di

i need to show a list of item in a ListBox from the bottom in WP7. So in case i have some items that the height sum of them is < of ListBox Height i need to have a blank item at the top with the difference of the Height.

I have to do this because i set the ItemSource of Listbox, so i cannot know what is the right height of all items before load them.

In Item_loaded event of every Item i save the height and at the last i need to set the Height of the First.

<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>

I set the ItemsSource and add a blank item at top, and a blank at bottom:

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

            ClassMessaggio FirstLineScrollMessage = new ClassMessaggio();
                            FirstLineScrollMess开发者_运维知识库age.IsSentMsg = "3";
                            messaggi.Insert(0, FirstLineScrollMessage); 

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

            this.ConvListBox.ItemsSource = messaggi;

And at Item_Loaded i'm trying this:

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;

            }

        }

My problem is that this line return me always null :(

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

I hope is clear, sorry for my english.

EDIT::

Ok this should work:

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


You can get to the first ListBoxItem using this code:

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

From there you can modify it's Height, etc.

Thanks, Stefan Wick - Microsoft Silverlight

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号