WPF 循環顯示列表

原文: WPF 循環顯示列表

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。 https://blog.csdn.net/SANYUNI/article/details/79423707

項目須要相似手機上設置時間的控件,能夠一直滾動顯示的內容連續的。在WPF中找到的列表控件只能滾到最後再反向滾動。css

基於ScrollViewer和StackPanel來改造,Xaml以下:web

<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代碼以下:markdown

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 < ShowItemCount; index++)
            {
                TextBlock text = 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) < DataSource.Count)
                {
                    startIndex += 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);

            }
        }

    }

測試界面

代碼連接地址svg