热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

WPF循环显示列表

原文:WPF循环显示列表版权声明:本文为博主原创文章,未经博主允许不得转载。https:blog.csdn.netSANYUNIarticledetails7942370
原文: WPF 循环显示列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SANYUNI/article/details/79423707

项目需要类似手机上设置时间的控件,可以一直滚动显示的内容连续的。在WPF中找到的列表控件只能滚到最后再反向滚动。

基于ScrollViewer和StackPanel来改造,Xaml如下:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="{Binding RelativeSource={RelativeSource AncestorType=local:ScrollList},Path=ItemHeight}"/>
            <RowDefinition/>
        Grid.RowDefinitions>
        <ScrollViewer x:Name="tt" Grid.RowSpan="3" PreviewMouseWheel="tt_PreviewMouseWheel" ScrollViewer.VerticalScrollBarVisibility="Hidden" >
            <StackPanel x:Name="stacktt" Background="Gray">
            StackPanel>
        ScrollViewer>

        <Rectangle Height="1" Fill="Red" Grid.Row="1" VerticalAlignment="Top"/>
        <Rectangle Height="1" Fill="Red" Grid.Row="1" VerticalAlignment="Bottom"/>
    Grid>

cs代码如下:

    public partial class ScrollList : UserControl
    {
        public ScrollList()
        {
            InitializeComponent();
            this.Loaded += ScrollList_Loaded;
        }

        private void ScrollList_Loaded(object sender, RoutedEventArgs e)
        {
            stacktt.Children.Clear();
            for (int index = 0; index new TextBlock() { Height=ItemHeight};
                stacktt.Children.Add(text);
            }
            RefreshData();
        }

        public List<int> DataSource
        {
            get { return (List<int>)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DataSource. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(List<int>), typeof(ScrollList), new PropertyMetadata(new List<int>()));




        public int SelectData
        {
            get { return (int)GetValue(SelectDataProperty); }
            set { SetValue(SelectDataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectData. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectDataProperty =
            DependencyProperty.Register("SelectData", typeof(int), typeof(ScrollList), new PropertyMetadata(0));



        public int ItemHeight
        {
            get { return (int)GetValue(ItemHeightProperty); }
            set { SetValue(ItemHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemHeight. This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemHeightProperty =
            DependencyProperty.Register("ItemHeight", typeof(int), typeof(ScrollList), new PropertyMetadata(20));



        int ShowItemCount { get {
                return (int)ActualHeight / ItemHeight;
            } }

        int startIndex = 0;


        private void tt_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            Console.WriteLine("TimeStap={0} Delta={1}", e.Timestamp, e.Delta);

            if (DataSource.Count == 0)
                return;

            if (e.Delta > 0)
            {
                if ((startIndex + ShowItemCount) 1;
                }
                else
                {
                    startIndex = 0;
                }
            }
            else
            {
                if ((startIndex - ShowItemCount) <0)
                {
                    startIndex = DataSource.Count - 1;
                }
                else
                {
                    startIndex -= 1;
                }

            }

            RefreshData();

        }

        private void RefreshData()
        {
            if (DataSource.Count > 0)
            {
                int count = 0;
                foreach (var item in stacktt.Children)
                {
                    if ((startIndex + count) > (DataSource.Count - 1))
                    {
                        (item as TextBlock).Text = DataSource[startIndex + count - DataSource.Count].ToString();
                    }
                    else
                    {
                        (item as TextBlock).Text = DataSource[startIndex + count].ToString();
                    }
                    count += 1;
                }
                TextBlock selectText = (TextBlock)VisualTreeHelper.GetChild(stacktt, ShowItemCount / 2);

                if (ShowItemCount%2 != 0)
                {
                    selectText = (TextBlock)VisualTreeHelper.GetChild(stacktt, ShowItemCount / 2+1);
                }
                SelectData = Convert.ToInt32(selectText.Text);

            }
        }

    }

测试界面

代码链接地址


推荐阅读
author-avatar
凯锐斯_372
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有