第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在聊天中對齊 ListView 項目

在聊天中對齊 ListView 項目

C#
斯蒂芬大帝 2022-07-23 16:25:26
現(xiàn)在,這個聊天機器人將其所有顯示文本都向左對齊。用戶輸入應該向右對齊,但不知道如何做到這一點。我嘗試向 中添加一個HorizontalAlignment="Right",ListView但它會將整個機器人/用戶聊天列表移動到右側。我只希望用戶輸入的文本右對齊。但是怎么做?我還查找了不同ItemTemplate的選擇,ListView但沒有一個有我需要的。我會接受 XAML 中的解決方案或 C# 中的編程方式。主頁.xaml<Page    x:Class="StudyBot.MainPage"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:local="using:StudyBot"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">    <Page.Resources>        <DataTemplate x:Key="MessagesListDataTemplate">            <Grid x:Name="Text" Background="White" VerticalAlignment="Center" Margin="5, 5, 5, 5">                <Border BorderThickness="2" BorderBrush="deepskyblue" CornerRadius="7">                    <TextBlock Text="{Binding Text}" TextWrapping="Wrap" Margin="5, 5, 5, 5" />                </Border>            </Grid>        </DataTemplate>    </Page.Resources>    <Grid HorizontalAlignment="Center" VerticalAlignment="Stretch" >        <Grid.ColumnDefinitions>            <ColumnDefinition Width="1.3*"/>            <ColumnDefinition Width="5*"/>        </Grid.ColumnDefinitions>        <Grid.RowDefinitions>            <RowDefinition Height="0.9*"/>            <RowDefinition Height="*"/>        </Grid.RowDefinitions>        <Border Background="#2f5cb6"/>        <Border Grid.Column ="1" Background="#1f3d7a"/>        <Border Grid.Row="1" Grid.ColumnSpan="2" Background="#152951"/>
查看完整描述

2 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

找到了一個有效的 C#、XAML 組合。我將此添加到我的MainPage():


// Add an event handler for the ContainerContentChanging event of the ListView

MessagesList.ContainerContentChanging += OnContainerContentChanging;

在同一個文件中添加了這個函數(shù):


private void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)

{

    if (args.InRecycleQueue) return;


    // Currently we are adding messages to the ListView.ItemSource as Activity objects

    // since this handler is called when the content changes (an item is added)

    // intercept the item as an activity and set its horizontal alignment accordingly

    Activity message = args.Item as Activity;

    args.ItemContainer.HorizontalAlignment = (message.From.Name == botHandle) ? HorizontalAlignment.Right : HorizontalAlignment.Left;

}

上面的代碼對應于ListView上面文件的 XAML 中的我的(及其模板):


<Page.Resources>

    <DataTemplate x:Key="MessagesListDataTemplate">

        <Grid x:Name="TextB" Background="White" VerticalAlignment="Center" Margin="5, 5, 5, 5">

            <Border>

                <TextBlock Text="{Binding Text}" TextWrapping="Wrap" MaxWidth="500" />

            </Border>

        </Grid>

    </DataTemplate>

</Page.Resources>


<ListView x:Name="MessagesList" Grid.Row="1" ItemTemplate="{StaticResource MessagesListDataTemplate}" Margin="5, 5, 5, 15"/>


查看完整回答
反對 回復 2022-07-23
?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您可以在模型中添加一個附加屬性,例如IsUserText,然后創(chuàng)建兩個TextBlock左對齊和另一個右對齊,您可以根據(jù)屬性設置VisibilityTextBlock 的IsUserText屬性。


你DataTemplate應該看起來像這樣:


<DataTemplate x:Key="MessagesListDataTemplate">

   <Grid x:Name="Text" Background="White" VerticalAlignment="Center" Margin="5, 5, 5, 5">

     <Border BorderThickness="2" BorderBrush="deepskyblue" CornerRadius="7">

           <TextBlock Text="{Binding Text}" TextWrapping="Wrap" HorizontalAlignment="Left" Visibility={Binding IsUserText, Converter={StaticResource InverserBoolToVisibilityConverter}} Margin="5, 5, 5, 5" />

     </Border>

    <Border BorderThickness="2" BorderBrush="deepskyblue" CornerRadius="7">

            <TextBlock Text="{Binding Text}" TextWrapping="Wrap" HorizontalAlignment="Right" Visibility={Binding IsUserText, Converter={StaticResource BoolToVisibilityConverter}} Margin="5, 5, 5, 5" />

     </Border>

   </Grid>

</DataTemplate>

編輯:


另外,如果您不想使用 two TextBlock's,您可以編寫轉換器 forHorizontalAlignment并且可以設置LeftorRight基于'IsUserText屬性


查看完整回答
反對 回復 2022-07-23
  • 2 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號