首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

在SL/WPF中实现列表的滚动显示解决办法

2013-01-25 
在SL/WPF中实现列表的滚动显示以前是做ASP.NET网页开发的,在页面上需要实现列表的滚动显示时间常用JS轻松

在SL/WPF中实现列表的滚动显示
以前是做ASP.NET网页开发的,在页面上需要实现列表的滚动显示时间常用JS轻松的完成了,但在WPF中却很难,我尝试过用listbox,但有两个问题,
一是滚动用生硬(一跳一跳的,而且样式不好控制),
二是,我想单击标题弹出内容,我用listbox的selectionChange事件完成,但单击后item是选中状态,需要再点一次之后,才能激动事件....

希望前辈帮忙给个思路,如果有示例代码就最好,我就是想实现一个新闻列表的滚动显示,并能单击新闻标题弹出新闻内容的功能,感谢各位了
[解决办法]
在XAML里:
<StackPanel Name="spScrollNews" Orientation="Horizontal" HorizontalAlignment="Left">
                <!--等待动态添加-->
   </StackPanel>

在C#里:

  private System.Windows.Threading.DispatcherTimer dt = new DispatcherTimer();
        private int scrollNewsSwidth=2;
   
         private void LoadNews()
            {

                IQueryable<dms_News> importantNews  = rp.GetAllNews(true).Take(15); ;
                IQueryable<dms_News> normalNews = rp.GetAllNews(false).Take(15);
  
                for(int i=1;i<16;i++)
                {
                    showImportantNews(importantNews, i);
                    showNormalNews(normalNews, i);
                }
             //让最新(修改)的一普通新闻内容在顶部滚动
                showScrollNews(normalNews);
                spScrollNews.Width = scrollNewsSwidth;
                spScrollNews.Margin = new Thickness(this.Width, 0, 0, 0);
             //滚动开始
                dt.Interval = TimeSpan.FromMilliseconds(100);
                dt.Tick += new EventHandler(dt_Tick);
                dt.Start();
            }

         private void showScrollNews(IQueryable<dms_News> normalNews)
         {
             var firstNews = normalNews.FirstOrDefault();
             System.IO.StringReader sr = new System.IO.StringReader(firstNews.Content);
             System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);
             FlowDocument fd = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);


             TextRange tr = new TextRange(fd.ContentStart, fd.ContentEnd);
             string content=tr.Text.Replace('\r', ' ').Replace('\n', ' ');
             TextBlock scrollNews = new TextBlock();
             scrollNews.Text = string.Concat(firstNews.Title, ": ",content );
             spScrollNews.Children.Add(scrollNews);
             scrollNewsSwidth += scrollNews.Text.Count() * 12;
         }
  private void showNormalNews(IQueryable<dms_News> normalNews, int i)
         {
             if (i <= normalNews.Count())
             {
                 if (i < 9)
                 {
                     NewsTextBlock tbn = new NewsTextBlock();
                     tbn.news = normalNews.Skip(i - 1).FirstOrDefault();
                     tbn.ToolTip = "发布时间:" + tbn.news.CreatTime;
                     tbn.Text = string.Concat(i, ". ", tbn.news.Title);
                     tbn.Margin = new Thickness(6);
                     tbn.MouseLeftButtonDown += new MouseButtonEventHandler(tb_MouseLeftButtonDown);
                     spNormalNews.Children.Add(tbn);
                    
                 }
                 /********添加至滚动新闻标题Start*******
                 NewsTextBlock tbnScroll = new NewsTextBlock();
                 tbnScroll.news = normalNews.Skip(i - 1).FirstOrDefault();
                 tbnScroll.ToolTip = "发布时间:" + tbnScroll.news.CreatTime;
                 tbnScroll.Text = tbnScroll.news.Title;
                 tbnScroll.Margin = new Thickness(5, 2, 25, 2);


                 tbnScroll.MouseLeftButtonDown += new MouseButtonEventHandler(tb_MouseLeftButtonDown);
                 tbnScroll.MouseMove += new MouseEventHandler(tb_MouseMove);
                 tbnScroll.MouseLeave += new MouseEventHandler(tb_MouseLeave);
                 spScrollNews.Children.Add(tbnScroll);
                 scrollNewsSwidth += 30 + tbnScroll.Text.Count() * 12;
                 ********添加至滚动新闻标题End******* */
             }
         }

         private void showImportantNews(IQueryable<dms_News> importantNews, int i)
         {
             if (i <= importantNews.Count())
             {
                 if (i < 9)
                 {
                     NewsTextBlock tb = new NewsTextBlock();
                     tb.news = importantNews.Skip(i - 1).FirstOrDefault(); ;
                     tb.ToolTip = "发布时间:" + tb.news.CreatTime;
                     tb.Text = string.Concat(i, ". ", tb.news.Title);
                     tb.Margin = new Thickness(6);
                     //spImportantNews.Width += 30 + p.Myname.Count() * 13;
                     tb.MouseLeftButtonDown += new MouseButtonEventHandler(tb_MouseLeftButtonDown);
                     spImportantNews.Children.Add(tb);
                    // scrollNewsSPcount++;
                 }
                 /********添加至滚动新闻标题Start*******
                NewsTextBlock tbScroll = new NewsTextBlock();
                tbScroll.news = importantNews.Skip(i - 1).FirstOrDefault(); ;


                tbScroll.ToolTip = "发布时间:" + tbScroll.news.CreatTime;
                tbScroll.Text = tbScroll.news.Title;
                tbScroll.Margin = new Thickness(5, 2, 25, 2);
                tbScroll.MouseLeftButtonDown += new MouseButtonEventHandler(tb_MouseLeftButtonDown);
                tbScroll.MouseMove += new MouseEventHandler(tb_MouseMove);
                tbScroll.MouseLeave += new MouseEventHandler(tb_MouseLeave);
                spScrollNews.Children.Add(tbScroll);
                scrollNewsSwidth += 30 + tbScroll.Text.Count() * 12;
                  *  ********添加至滚动新闻标题End******* */
             }
         }

        private void tb_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
         {
             InfoShowWindows.ShowNewsDetailsWindow sndw =
                 new InfoShowWindows.ShowNewsDetailsWindow((sender as NewsTextBlock).news);
             sndw.ShowDialog();
         }

       private  void dt_Tick(object sender, EventArgs e)
         {
             if (spScrollNews.Width + spScrollNews.Margin.Left > 0 )//
[解决办法]
 ws + spScrollNews.Margin.Left > 0)
             {
                 spScrollNews.Margin = new Thickness(spScrollNews.Margin.Left - 2, 0, 0, 0);
             }
             else
             {
                 spScrollNews.Margin = new Thickness(this.Width, 0, 0, 0);
                dt.Stop();
                
                 //刷新新闻
                dt.Stop();


                dt.Tick -= new EventHandler(dt_Tick);
                 scrollNewsSwidth = 20;
                 spScrollNews.Children.Clear();
                 spImportantNews.Children.Clear();
                 spNormalNews.Children.Clear();
                 LoadNews();
           
             }

         }

       private  void tb_MouseMove(object sender, MouseEventArgs e)
         {
             dt.Stop();
         }

        private void tb_MouseLeave(object sender, MouseEventArgs e)
         {
             dt.Start();
         }
    }

    public class NewsTextBlock : TextBlock
    {
        public dms_News news { get; set; }
    }

热点排行