wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了xaml – 通用App ListView项目Horizo​​ntalAlignment大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我想创建一个ListView,它具有右对齐的项目以及左对齐的项目.到目前为止,在我使用DataTemplates和ItemContainerStyles的所有尝试中,我都无法实现这一点.左对齐工作正常,因为这是默认值,但我无法弄清楚如何使一些项目右对齐.例如,这类似于 Windows Phone messaging应用程序之类的聊天/对话类型视图. 我目前的XAML看起来像这样: <Page x:
我想创建一个ListView,它具有右对齐的项目以及左对齐的项目.到目前为止,在我使用DataTemplates和ItemContainerStyles的所有尝试中,我都无法实现这一点.左对齐工作正常,因为这是认值,但我无法弄清楚如何使一些项目右对齐.例如,这类似于 Windows Phone messaging应用程序之类的聊天/对话类型视图.

我目前的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有点新意,如果我离开这里,请原谅我.

更新:解决方

网格是关键,我最终必须使用多个网格来实现正确的右对齐,并结合设置Horizo​​ntalContentAlignment的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>

解决方法

问题出在您的DataTemplates中,而不是样式等.您必须在DataTemplate中使用Grid而不是Stackpanel来实现这一点.

Stackpanels不会伸展到parent.y将只获得其中所有控件的宽度/高度.尝试类似的东西

<Grid.columnDeFinitions>
        <columnDeFinition Width="*" />
        <columnDeFinition Width="*" />
        <columnDeFinition Width="*" />
    </Grid.columnDeFinitions>

大佬总结

以上是大佬教程为你收集整理的xaml – 通用App ListView项目Horizo​​ntalAlignment全部内容,希望文章能够帮你解决xaml – 通用App ListView项目Horizo​​ntalAlignment所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。