C# 相对面板/网格无法在项目模板选择器中对齐水平对齐
我正在开发聊天应用程序。聊天页面包含一个itemTemplateSelector,它通过检查布尔值,根据发送者将文本与右端/左端对齐。这是密码C# 相对面板/网格无法在项目模板选择器中对齐水平对齐,c#,xaml,grid,uwp,itemtemplateselector,C#,Xaml,Grid,Uwp,Itemtemplateselector,我正在开发聊天应用程序。聊天页面包含一个itemTemplateSelector,它通过检查布尔值,根据发送者将文本与右端/左端对齐。这是密码 <Page.Resources> <DataTemplate x:Key="ChatTemplateR"> <Grid HorizontalAlignment="Stretch"> <StackPanel HorizontalAlignment="Right" &g
<Page.Resources>
<DataTemplate x:Key="ChatTemplateR">
<Grid HorizontalAlignment="Stretch">
<StackPanel HorizontalAlignment="Right" >
<Border Background="{ThemeResource SystemControlBackgroundAccentBrush}" >
<TextBlock MinWidth="200" Text="{Binding conversation}" TextWrapping="Wrap" Margin="5"/>
</Border>
<Path x:Name="DownRightTri"
HorizontalAlignment="Right"
Margin="0,0,10,0"
Fill="{ThemeResource SystemControlBackgroundAccentBrush}"
Data="M0,0 H10 V10" />
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="ChatTemplateL">
<Grid HorizontalAlignment="Stretch">
<StackPanel HorizontalAlignment="Left">
<Path x:Name="UpLeftTri"
HorizontalAlignment="Left"
Margin="10,0,0,0"
Fill="{ThemeResource SystemControlBackgroundAccentBrush}"
Data="M0,-5 V5 H10 " />
<Border Background="{ThemeResource SystemControlBackgroundAccentBrush}" >
<TextBlock MinWidth="200" Text="{Binding conversation}" TextWrapping="Wrap" Margin="5"/>
</Border>
</StackPanel>
</Grid>
</DataTemplate>
<local1:ChatTemplateSelector x:Key="ChatSelector" LeftTemplate="{StaticResource ChatTemplateL}" RightTemplate="{StaticResource ChatTemplateR}"/>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton"
FontFamily="Segoe MDL2 Assets"
Content=""
Foreground="{StaticResource SystemControlBackgroundAccentBrush}"
FontSize="25"
Background="{StaticResource ApplicationPageBackgroundThemeBrush}"
Click="backButton_Click">
<Button.Transitions>
<TransitionCollection>
<EdgeUIThemeTransition Edge="Left"/>
</TransitionCollection>
</Button.Transitions>
</Button>
<TextBlock Text="Chats" Grid.Column="1" Style="{ThemeResource tb1}" HorizontalAlignment="Center"/>
<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<ListView x:Name="lv" ItemsSource="{Binding BuddyChatOC}" ItemTemplateSelector="{StaticResource ChatSelector}">
</ListView>
<RelativePanel Grid.Row="1" Margin="5,10,5,10">
<TextBox x:Name="sendtext" Margin="0,0,2,0" RelativePanel.AlignLeftWithPanel="True" RelativePanel.LeftOf="sendtextbutton"/>
<Button x:Name="sendtextbutton" Content="Send" Command="{Binding sendChatCommand}" RelativePanel.AlignRightWithPanel="True" >
</Button>
</RelativePanel>
</Grid>
</Grid>
聊天框发生变化时,itemtemplateselector肯定能正常工作。我无法将右侧聊天框移动到右端。有什么建议吗?只需将
TextAlignment=“Right”
添加到右侧模板中的TextBlock
。您的ListView项目可能没有完全展开。。。尝试将此添加到ListView:
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
谢谢..但这只是移动了文本。你对如何将整个聊天泡泡向右移动有什么建议吗..就像windows phone中的消息应用程序一样,它必须根据屏幕大小移动到右角。
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>