简体   繁体   English

相对面板/网格无法在项目模板选择器中对齐水平对齐方式

[英]relative panel / grid cannot align the horizontal alignment in item template selector

I am working on a chat application. 我正在研究聊天应用程序。 The chat page consists of a itemTemplateSelector which aligns the text to right end/left end according to the sender by checking a bool value. 聊天页面由itemTemplateSelector组成,该ItemTemplateSelector通过检查bool值根据发送者将文本对齐到右端/左端。 here is the code 这是代码

<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="&#xE0E2;"
                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>

ItemTemplateSelecter: ItemTemplateSelecter:

public class ChatTemplateSelector : DataTemplateSelector
{
    public DataTemplate LeftTemplate { get; set; }
    public DataTemplate RightTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {

        BuddyChat2Datum chat = (BuddyChat2Datum)item;
        DataTemplate dt = chat.isLeft ? this.LeftTemplate : this.RightTemplate;
        return dt;
    }
}

在此处输入图片说明

The itemtemplateselector is working for sure as the chat boxes are changing. 随着聊天框的更改,itemtemplateselector肯定可以正常工作。 I am unable to move the rightSide chatBox to the right end. 我无法将rightSide chatBox移到右端。 Any suggestions? 有什么建议么?

Your ListView items are probably not stretching completely... Try adding this to ListView: 您的ListView项可能未完全拉伸...尝试将其添加到ListView中:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

只需在您的右侧模板中将TextAlignment="Right"添加到TextBlock中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM