我正在使用Microsoft.Xaml.Behaviors.Uwp.Managed NuGet程序包来控制UWP C#应用程序中的行为。我的ListView具有这样的自定义数据模板:
项目:
<ListView
ItemTemplate="{StaticResource ListViewMessageTemplate_Assistant}" x:Name="MessengerView"
BorderThickness="2,2,2,2" SelectionMode="None" Margin="10,10,10,10" IsMultiSelectCheckBoxEnabled="False" ScrollViewer.HorizontalScrollBarVisibility="Auto"
IsDoubleTapEnabled="False" IsHoldingEnabled="False"
IsRightTapEnabled="False" IsTapEnabled="False" ContainerContentChanging="MessengerView_ContainerContentChanging" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
数据模板:
<DataTemplate x:Key="ListViewMessageTemplate_Assistant">
<Grid x:Name="MainGrid" MaxWidth="500" Margin="10,0,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle x:Name="BackRect" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.RowSpan="2" VerticalAlignment="Stretch" RadiusX="20" RadiusY="20">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource Amazonite}"/>
</Rectangle.Fill>
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior x:Name="IsClient" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="0">
<core:ChangePropertyAction x:Name="Blue" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Cola Blue}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="NotIncludesData" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="1">
<core:ChangePropertyAction x:Name="Red" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Crimson Red}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="IsResultBalloon" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="2">
<core:ChangePropertyAction x:Name="Yellow" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource American Orange}"/>
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Rectangle>
<TextBlock x:Name="MessageText" HorizontalAlignment="Stretch" Margin="20,20,20,20" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message}" Grid.Row="1" RequestedTheme="Default" Grid.ColumnSpan="2"/>
<TextBlock x:Name="UserText" HorizontalAlignment="Stretch" Margin="10,10,5,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding User}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
<TextBlock x:Name="DateText" HorizontalAlignment="Stretch" Margin="5,10,10,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message_Time}" Grid.Column="1" TextAlignment="Center" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
</Grid>
</DataTemplate>
正如您在xaml代码中看到的那样,矩形颜色会根据给定的整数值更改其颜色,如下所示:
MessengerView.Items.Add(new MessageBaloon(("Hello", usr: MessageSender.Assistant, 3))); // Print the result.
class MessageBaloon
{
/// <summary>
/// Message sender enum types.
/// </summary>
public enum MessageSender
{
Client,
Assistant
}
private string message;
private MessageSender message_sender;
private string message_time;
private int message_style;
/// <summary>
/// Message.
/// </summary>
public string Message { get => message; }
/// <summary>
/// Get user.
/// </summary>
public MessageSender User { get => message_sender; }
/// <summary>
/// Time.
/// </summary>
public string Message_Time { get => message_time; }
/// <summary>
/// Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt.
/// </summary>
public int Message_style { get => message_style; set => message_style = value; }
/// <summary>
/// Creating a message baloon for chat UI based on given datas.
/// </summary>
/// <param name="msg">Message string.</param>
/// <param name="usr">User type. Use MessageSender enum please.</param>
/// <param name="style_number"> Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt. </param>
public MessageBaloon(string msg, MessageSender usr, int style_number )
{
message = msg;
message_sender = usr;
message_time = DateTime.Now.ToString();
message_style = style_number;
}
}
但是,正如您在本文中看到的那样,行为控制将颜色混合了一段时间。如果消息的气球的颜色必须根据绑定值变成浅绿色,则在一段时间内它会变成蓝色,而不会受到代码的干扰。您能帮我发现造成此问题的原因吗?谢谢。
答案 0 :(得分:1)
将此行为添加到模板中似乎可以解决此问题。以前,我不使用深绿色的触发器,因为我认为它是模板的默认背景颜色,如果不发生其他情况,则不会更改。
<core:DataTriggerBehavior x:Name="IsAssistant" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="3">
<core:ChangePropertyAction x:Name="Green" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Amazonite}"/>
</core:DataTriggerBehavior>