2 回答
TA贡献1860条经验 获得超8个赞
找到了一个有效的 C#、XAML 组合。我将此添加到我的MainPage():
// Add an event handler for the ContainerContentChanging event of the ListView
MessagesList.ContainerContentChanging += OnContainerContentChanging;
在同一个文件中添加了这个函数:
private void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
if (args.InRecycleQueue) return;
// Currently we are adding messages to the ListView.ItemSource as Activity objects
// since this handler is called when the content changes (an item is added)
// intercept the item as an activity and set its horizontal alignment accordingly
Activity message = args.Item as Activity;
args.ItemContainer.HorizontalAlignment = (message.From.Name == botHandle) ? HorizontalAlignment.Right : HorizontalAlignment.Left;
}
上面的代码对应于ListView上面文件的 XAML 中的我的(及其模板):
<Page.Resources>
<DataTemplate x:Key="MessagesListDataTemplate">
<Grid x:Name="TextB" Background="White" VerticalAlignment="Center" Margin="5, 5, 5, 5">
<Border>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" MaxWidth="500" />
</Border>
</Grid>
</DataTemplate>
</Page.Resources>
<ListView x:Name="MessagesList" Grid.Row="1" ItemTemplate="{StaticResource MessagesListDataTemplate}" Margin="5, 5, 5, 15"/>
TA贡献1813条经验 获得超2个赞
您可以在模型中添加一个附加属性,例如IsUserText,然后创建两个TextBlock左对齐和另一个右对齐,您可以根据属性设置VisibilityTextBlock 的IsUserText属性。
你DataTemplate应该看起来像这样:
<DataTemplate x:Key="MessagesListDataTemplate">
<Grid x:Name="Text" Background="White" VerticalAlignment="Center" Margin="5, 5, 5, 5">
<Border BorderThickness="2" BorderBrush="deepskyblue" CornerRadius="7">
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" HorizontalAlignment="Left" Visibility={Binding IsUserText, Converter={StaticResource InverserBoolToVisibilityConverter}} Margin="5, 5, 5, 5" />
</Border>
<Border BorderThickness="2" BorderBrush="deepskyblue" CornerRadius="7">
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" HorizontalAlignment="Right" Visibility={Binding IsUserText, Converter={StaticResource BoolToVisibilityConverter}} Margin="5, 5, 5, 5" />
</Border>
</Grid>
</DataTemplate>
编辑:
另外,如果您不想使用 two TextBlock's,您可以编写转换器 forHorizontalAlignment并且可以设置LeftorRight基于'IsUserText属性
- 2 回答
- 0 关注
- 94 浏览
添加回答
举报