Item中有属性来决定是左还是右,难点在于如何将ItemTemplate扩充占满整个宽度,这样子当界面缩放的时候,Item靠左或靠右也能随着移动,其实主要是下面的代码,设置ListViewItem的HorizontalContentAlignment属性为Stretch,铺满整个宽度,示例代码如下:

<ListView
    x:Name="LvmqttMsg"
    Background="Transparent"
    ItemsSource="{Binding MqttMsgItems}"
    ScrollViewer.CanContentScroll="False">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListView.ItemContainerStyle>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Border
                    HorizontalAlignment="{Binding Location}"
                    BorderBrush="#D291BC"
                    BorderThickness="2"
                    CornerRadius="10">
                    <StackPanel Margin="10">
                        <TextBox
                            Height="auto"
                            MaxWidth="300"
                            Text="{Binding Msg}"
                            TextWrapping="Wrap" />
                        <TextBlock Margin="0,5,0,0" Text="{Binding Time}" />
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>