gpt4 book ai didi

wpf 自动滚动数据绑定(bind) ItemsControl 中的项目(如新闻自动收报机)

转载 作者:行者123 更新时间:2023-12-04 06:20:17 25 4
gpt4 key购买 nike

我正在为我的应用程序创建一个消息代码。

 <UserControl.Resources>
<DataTemplate x:Key="MessagesDataTemplate">
<TextBlock
FontWeight="Bold"
FontSize="12"
Text="{Binding Path=Message}"
Height="30"
Margin="2"/>
</DataTemplate>
</UserControl.Resources>

<Grid>
<ItemsControl
ItemsSource="{Binding Messages}"
ItemTemplate="{StaticResource MessagesDataTemplate}">
</ItemsControl>
</Grid>

我想显示一个消息项目(即 TextBlock)5 秒钟,然后移动到下一个项目(垂直)。

有人可以指导我吗?

最佳答案

我创建了一个示例
这是xaml和背后的代码

<UserControl x:Class="WpfApplication1.MessageTicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="30">
<UserControl.Resources>
<DataTemplate x:Key="MessagesDataTemplate">
<Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
<Grid.Resources>
<Storyboard x:Key="sb2">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" >
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" >
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" >
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)">
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="2"/>
<EasingDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Grid.Resources>
<Grid.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<Grid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource sb2}">

</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
<TextBlock
FontWeight="Bold"
FontSize="12"
Text="{Binding Path=Message}"
Height="30"
Margin="2"/>
</Grid>
</DataTemplate>
</UserControl.Resources>

<Grid>
<ListBox BorderThickness="0" BorderBrush="Transparent" Name="messagesLB"
ItemsSource="{Binding Messages}"
ItemTemplate="{StaticResource MessagesDataTemplate}">
</ListBox>
</Grid>
</UserControl>

代码背后
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MessageTicker.xaml
/// </summary>
public partial class MessageTicker : UserControl
{
public MessageTicker()
{
InitializeComponent();
List<Temp> Messages = new List<Temp>();
for (int i = 0; i < 10; i++)
{
Messages.Add(new Temp { Message = "Message" + i });
}
messagesLB.ItemsSource = Messages;
DispatcherTimer dt = new DispatcherTimer();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = TimeSpan.FromSeconds(2);
dt.Start();
}

void dt_Tick(object sender, EventArgs e)
{
if ((messagesLB.SelectedIndex + 1) < messagesLB.Items.Count)
messagesLB.SelectedIndex = messagesLB.SelectedIndex + 1;
else
messagesLB.SelectedIndex = 0;
messagesLB.ScrollIntoView(messagesLB.SelectedItem);
}

public class Temp
{
public string Message { get; set; }
}
}


}

关于wpf 自动滚动数据绑定(bind) ItemsControl 中的项目(如新闻自动收报机),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6676227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com