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");
私の英語で申し訳ありませんが、明確であることを願っています。
編集::
これはうまくいくはずです:
var Baloons = LayoutRoot.GetVisualDescendants().OfType<Border>();
foreach (var FirstBaloon in Baloons)
{
if (FirstBaloon != null)
{
if (FirstBaloon.Name == "FirstScrollBaloon")
{
FirstBaloon.Height = ConvListBox.ActualHeight - TotalBaloonsHeight;
break;
}
}
}