Windows 8在数据绑定后获得ItemsControl宽度

时间:2014-02-10 10:53:33

标签: c# xaml windows-8 windows-runtime winrt-xaml

我正在使用Itemcontrol创建一个Windows 8应用程序,并绑定一个数据列表以显示新闻。我需要创建像HTML中的新闻自动收录器一样的动画,如链接中所示:http://coolcarousels.frebsite.nl/c/9/ 我将如何实现这一目标?

代码:

 <ItemsControl x:Name="lstdata"  >
                <ItemsControl.ItemTemplate  >
                    <DataTemplate   >
                        <!--Date-->
                        <StackPanel Orientation="Vertical" Margin="10,0" HorizontalAlignment="Left" VerticalAlignment="Center" >
                            <TextBlock HorizontalAlignment="Left" Text="{Binding Path=ModifedDate,Converter={StaticResource DatetimeToStringFormatConverter}}" VerticalAlignment="Top" Grid.Row="1"  FontFamily="Segoe UI" FontWeight="Bold" FontSize="13.33" />
                            <TextBlock   Margin="0,0,0,0" Text="{Binding Path=Title , Mode=TwoWay}" Grid.Row="3" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="Segoe UI" FontSize="13.33" />
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>


var  newsViewModel = new  NewsViewModel();
          await  newsViewModel.GetNews();
          lstdata.ItemsSource = newsViewModel.NewsList;

动画:

FlipViewNews.RenderTransform = new TranslateTransform();

            DoubleAnimation animateX = new DoubleAnimation();
            animateX.From = 0;
            animateX.To = 200;
            animateX.Duration = TimeSpan.FromMilliseconds(400);

            Storyboard.SetTarget(animateX, FlipViewNews.RenderTransform);
            Storyboard.SetTargetProperty(animateX, "TranslateTransform.X");

            Storyboard story = new Storyboard();
            story.Children.Add(animateX);
            story.RepeatBehavior = RepeatBehavior.Forever;
            story.Begin();

我已经能够制作动画了。上面的代码工作唯一的问题是,我需要获得所有项目绑定后的ItemsControl实际宽度。我怎么能这样,以便我可以在HTML中创建像marquee一样的动画?

1 个答案:

答案 0 :(得分:0)

你可以添加一个SizeChanged侦听器,然后在绑定影响大小时获取元素的ActualWidth吗?

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.sizechanged.aspx