原文:
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 < 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);
}
}
}