大佬教程收集整理的这篇文章主要介绍了xaml – 通用App ListView项目HorizontalAlignment,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前的XAML看起来像这样:
<Page x:Class="MDControl.MainPage" xmlns="http://scheR_702_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_702_11845@as.microsoft.com/winfx/2006/xaml" xmlns:local="using:MDControl" xmlns:d="http://scheR_702_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_702_11845@as.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.resources> <CollectionViewsource x:Name="messages" source="{Binding mmessages}"/> <DataTemplate x:Key="SentmessageTemplate"> <StackPanel Padding="10" Margin="5" BACkground="Teal" HorizontalAlignment="Right" Width="Auto"> <TextBlock Text="{Binding messageTypE}" FontWeight="Bold" textwrapping="Nowrap" Foreground="White"/> <TextBlock Text="{Binding messageBody}" textwrapping="Wrap" Foreground="White" /> <TextBlock Text="{Binding timestamp}" textwrapping="Nowrap" Foreground="White" FontStyle="Italic" FontSize="12"/> </StackPanel> </DataTemplate> <DataTemplate x:Key="ReceivedmessageTemplate"> <StackPanel Padding="10" Margin="5" BACkground="LightGray"> <TextBlock Text="{Binding messageTypE}" FontWeight="Bold" textwrapping="Nowrap"/> <TextBlock Text="{Binding messageBody}" textwrapping="Wrap"/> <TextBlock Text="{Binding timestamp}" textwrapping="Nowrap" TextAlignment="Right" FontStyle="Italic" FontSize="12"/> </StackPanel> </DataTemplate> <Style TargetType="ListViewItem" x:Key="SentmessageStyle"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> <Style TargetType="ListViewItem" x:Key="ReceivedmessageStyle"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> <local:messageListTemplate@R_696_10288@ctor x:Key="messageListTemplate@R_696_10288@ctor" SentmessageTemplate="{Staticresource SentmessageTemplatE}" ReceivedmessageTemplate="{Staticresource ReceivedmessageTemplatE}"> </local:messageListTemplate@R_696_10288@ctor> <local:messageListContainerStyle@R_696_10288@ctor x:Key="messageListContainerStyle@R_696_10288@ctor" SentmessageStyle="{Staticresource SentmessageStylE}" ReceivedmessageStyle="{Staticresource ReceivedmessageStylE}"> </local:messageListContainerStyle@R_696_10288@ctor> </Page.resources> <Grid BACkground="{Themeresource ApplicationPageBACkgroundThemeBrush}"> <ListView x:Name="messageList" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemContainerStyle@R_696_10288@ctor="{Staticresource messageListContainerStyle@R_696_10288@ctor}" Itemssource="{Binding source={Staticresource messages}}" ItemTemplate@R_696_10288@ctor="{Staticresource messageListTemplate@R_696_10288@ctor}" Margin="10,120,10,50" VerticalAlignment="Bottom" IsDoubleTapEnabled="false"/> </Grid>
我可以更改什么以使“已发送”消息正确对齐?目前它们出现了我想要的蓝绿色背景,但它们仍然是左对齐而不是右对齐.我对XAML有点新意,如果我离开这里,请原谅我.
更新:解决方案
网格是关键,我最终必须使用多个网格来实现正确的右对齐,并结合设置HorizontalContentAlignment的ItemContainerStyle.
<Page x:Class="MDControl.MainPage" xmlns="http://scheR_702_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_702_11845@as.microsoft.com/winfx/2006/xaml" xmlns:local="using:MDControl" xmlns:d="http://scheR_702_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_702_11845@as.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Page.resources> <CollectionViewsource x:Name="messages" source="{Binding mmessages}"/> <DataTemplate x:Key="SentmessageTemplate"> <Grid> <Grid.RowDeFinitions> <RowDeFinition Height="Auto"/> </Grid.RowDeFinitions> <Grid Height="Auto" Grid.Row="1" Margin="5" HorizontalAlignment="Right"> <Grid.columnDeFinitions> <columnDeFinition Width="*"/> </Grid.columnDeFinitions> <StackPanel Padding="10" BACkground="Teal"> <TextBlock Text="{Binding messageTypE}" FontWeight="Bold" textwrapping="Nowrap" Foreground="White" /> <TextBlock Text="{Binding messageBody}" textwrapping="Wrap" Foreground="White" /> <TextBlock Text="{Binding timestamp}" textwrapping="Nowrap" Foreground="White" FontStyle="Italic" FontSize="12" HorizontalAlignment="Right"/> </StackPanel> </Grid> </Grid> </DataTemplate> <DataTemplate x:Key="ReceivedmessageTemplate"> <Grid> <Grid.RowDeFinitions> <RowDeFinition Height="Auto"/> </Grid.RowDeFinitions> <Grid Height="Auto" Grid.Row="1" Margin="5" HorizontalAlignment="Left"> <Grid.columnDeFinitions> <columnDeFinition Width="*"/> </Grid.columnDeFinitions> <StackPanel Padding="10" BACkground="LightGray"> <TextBlock Text="{Binding messageTypE}" FontWeight="Bold" textwrapping="Nowrap" /> <TextBlock Text="{Binding messageBody}" textwrapping="Wrap" /> <TextBlock Text="{Binding timestamp}" textwrapping="Nowrap" FontStyle="Italic" FontSize="12" HorizontalAlignment="Right"/> </StackPanel> </Grid> </Grid> </DataTemplate> <local:messageListTemplate@R_696_10288@ctor x:Key="messageListTemplate@R_696_10288@ctor" SentmessageTemplate="{Staticresource SentmessageTemplatE}" ReceivedmessageTemplate="{Staticresource ReceivedmessageTemplatE}"> </local:messageListTemplate@R_696_10288@ctor> </Page.resources> <Grid BACkground="{Themeresource ApplicationPageBACkgroundThemeBrush}"> <ListView x:Name="messageList" ScrollViewer.VerticalScrollBarVisibility="Visible" Itemssource="{Binding source={Staticresource messages}}" ItemTemplate@R_696_10288@ctor="{Staticresource messageListTemplate@R_696_10288@ctor}" Margin="10,50" VerticalAlignment="Bottom" IsDoubleTapEnabled="false"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListView.ItemContainerStyle> </ListView> </Grid>
以上是大佬教程为你收集整理的xaml – 通用App ListView项目HorizontalAlignment全部内容,希望文章能够帮你解决xaml – 通用App ListView项目HorizontalAlignment所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。