所以我有一个带有文本框的longlistselector。用户可以与这些文本框进行交互,并插入更多文本。插入文本后,文本框会展开,用户可以继续编写并进行概述。
但是当用户到达longlistselector的底部并开始在最后一个文本框中写入时,文本框会在longlistselector外部扩展。这意味着用户必须停止写入并向下滚动longlistselector,然后继续写入。
所以问题是,当文本框位于longlistselector的底部并且文本扩展到视图外时,longlistselector不会向下滚动。
这是我的longlistselector的xaml代码
<Grid x:Name="ContentPanel" Margin="12,49,12,0" Grid.RowSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<phone:LongListSelector x:Name="ChatList" Grid.Row="0" HorizontalAlignment="Left" Width="456" Padding="0" >
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<local:ChatRoomTemplateSelector Content="{Binding}">
<local:ChatRoomTemplateSelector.YourSelf>
<DataTemplate x:Name="YourSelf">
<StackPanel>
<Grid x:Name="YourSelfGrid">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<UserControl Grid.Row="0">
<Path Data="Data" Fill="LawnGreen" StrokeThickness="0" Stretch="Fill" UseLayoutRounding="True"/>
</UserControl>
<TextBox Background="LawnGreen" Text="{Binding Path=Post, Mode=TwoWay,UpdateSourceTrigger=Explicit}" IsReadOnly="{Binding readOnly}" FontSize="20" Margin="39,10" TextWrapping="Wrap" BorderBrush="LawnGreen" Style="{StaticResource TextBoxStyleYourself}" TextChanged="OnTextBoxTextChanged"/>
</Grid>
<StackPanel Orientation="Horizontal">
<Path Data="Data" Fill="LawnGreen" StrokeThickness="0" Stretch="Fill" UseLayoutRounding="True" Margin="50,-1,0,0"/>
<TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="25" TextWrapping="Wrap"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</local:ChatRoomTemplateSelector.YourSelf>
</local:ChatRoomTemplateSelector>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
</Grid>
我使用代码片段
向longlistselector添加内容source.Add(new Data() { Name = ChatRoomOverview.userName, User = "YourSelf", readOnly = false, Post = "" });
ChatList.ItemsSource = source;
修改
我知道如何在启动时滚动到最后一个元素并且LLS工作正常。但是当最后插入的元素中的用户输入大量文本以使文本框在视图外扩展时,我无法获得LLS以保持项目在视图中。我该怎么做?
答案 0 :(得分:0)
如果您以编程方式添加项目,则还需要以编程方式确保将它们滚动到视图中。您可以使用LongListSelector.ScrollTo
方法执行此操作
LLS不会自动将新添加的项目滚动到视图中。
<强>更新强>
作为LLS问题底部扩展文本框的解决方案。您可以改为执行以下操作:
XAML:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer x:Name="MyScrollViewer">
<StackPanel>
<ItemsControl x:Name="MyItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}"
TextWrapping="Wrap"
Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBox AcceptsReturn="True"
FontSize="36"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
Text="{Binding}"
TextChanged="OnTextChanged"
Margin="0,0,0,48" />
</StackPanel>
</ScrollViewer>
</Grid>
代码背后:
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
this.InitializeComponent();
// Some sample existing items
this.MyItems.ItemsSource = new[] { "one", "two", "three", "four", "five" };
}
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
// This is a brute force attempt to scroll to the bottom of all content.
// You may want to be smarter about when this is done, such as when a "\r"
// is added to the end of the text in the textbox or the text is added
// such that it forces a new line. Beware text being changed in the middle
// of a large block of text as may not want that to trigger a scroll to the bottom.
this.MyScrollViewer.ScrollToVerticalOffset(double.MaxValue);
}
}
有点粗糙,但希望你明白了。
这在ItemsControl
内部使用单独的TextBox
和ScrollViewer
来模仿LLS行为,并且会一直展开,保持TextBox的底部可见,同时仍然允许滚动所有内容。<登记/>
显然,如果你与LLS密切相关,这将无效。