2011-07-13 35 views
2

私のアプリケーション用のメッセージティッカーを作成しています。wpf自動でデータバインドされたアイテムItemsControl(ニュースティッカーのようなもの)

<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秒間表示してから、次のアイテム(垂直方向)に移動します。

誰も私にこの案内を教えてもらえますか?

+0

の後ろあなたはItemsControlにでアイテムをハイライト表示したいあなたは、次を強調したい5秒後にアイテムコントロールのアイテム..? – Bathineni

+0

あまり...しかし、ItemsControlを使用するのが正しいものではないと私は考えています。理由は、UserControlがBorderの内側にあり、1つのMessageだけが見えるように境界の高さが設定されるためです。私が望むのは、5秒の遅れでメッセージを垂直方向にスクロールすることです。ドン...質問をする前にこれをよく考えていたはずです。 – empo

答えて

2

私はここにサンプル を作成しているが、そのための背後にある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; } 
     } 
    } 


} 
+0

すてきなこと...どのようにして滑りやすいのか効果? – empo

+0

私は答えでxamlファイルを更新しました....私はいくつかの愚かなアニメーションを追加しました。あなたは好きなようにそれを変更することができます.. – Bathineni

+0

おかげさまで、お風呂のお返事ありがとうございました。特に愚かなアニメーション;) – empo

関連する問題