Làm cách nào để tạo hiệu ứng cuộn cho ListBox? Tôi biết tôi có thể sử dụng scrollIntoView nhưng làm thế nào tôi có thể animate nó? Tôi muốn nhấn các phím mũi tên để di chuyển từ một listBoxItem này sang một danh sách khác.Di chuyển hình động
Trả lời
Đây là một thực hiện thô dựa trên phương pháp tương tự như liên kết sau
http://aniscrollviewer.codeplex.com/
Thuộc tính VerticalOffset
là read-only để thay vào đó bạn có thể sử dụng một tài sản VerticalOffset
gắn trên ScrollViewer
mà lần lượt làm ScrollToVerticalOffset
. Thuộc tính đính kèm này có thể được làm động.
Bạn cũng có thể tạo phương thức tiện ích mở rộng cho ItemsControl
được gọi là AnimateScrollIntoView
.
Gọi nó như
listBox.AnimateScrollIntoView(yourItem);
ScrollViewerBehavior
nàypublic class ScrollViewerBehavior
{
public static DependencyProperty VerticalOffsetProperty =
DependencyProperty.RegisterAttached("VerticalOffset",
typeof(double),
typeof(ScrollViewerBehavior),
new UIPropertyMetadata(0.0, OnVerticalOffsetChanged));
public static void SetVerticalOffset(FrameworkElement target, double value)
{
target.SetValue(VerticalOffsetProperty, value);
}
public static double GetVerticalOffset(FrameworkElement target)
{
return (double)target.GetValue(VerticalOffsetProperty);
}
private static void OnVerticalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
ScrollViewer scrollViewer = target as ScrollViewer;
if (scrollViewer != null)
{
scrollViewer.ScrollToVerticalOffset((double)e.NewValue);
}
}
}
ItemsControlExtensions
public static class ItemsControlExtensions
{
public static void AnimateScrollIntoView(this ItemsControl itemsControl, object item)
{
ScrollViewer scrollViewer = VisualTreeHelpers.GetVisualChild<ScrollViewer>(itemsControl);
UIElement container = itemsControl.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
int index = itemsControl.ItemContainerGenerator.IndexFromContainer(container);
double toValue = scrollViewer.ScrollableHeight * ((double)index/itemsControl.Items.Count);
Point relativePoint = container.TranslatePoint(new Point(0.0, 0.0), Window.GetWindow(container));
DoubleAnimation verticalAnimation = new DoubleAnimation();
verticalAnimation.From = scrollViewer.VerticalOffset;
verticalAnimation.To = toValue;
verticalAnimation.DecelerationRatio = .2;
verticalAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(verticalAnimation);
Storyboard.SetTarget(verticalAnimation, scrollViewer);
Storyboard.SetTargetProperty(verticalAnimation, new PropertyPath(ScrollViewerBehavior.VerticalOffsetProperty));
storyboard.Begin();
}
}
Và kể từ khi bạn cũng cần phải được một tổ chức của ScrollViewer
bạn sẽ cần
public static class VisualTreeHelpers
{
public static T GetVisualChild<T>(DependencyObject parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
}
Hãy xem article này, nó giải thích cách cuộn động và thêm cử chỉ cảm ứng. Tải xuống nguồn ở cuối trang và xem giải pháp WpfScrollContent. Tôi sẽ mở rộng ListBox WPF và thêm các hình ảnh động cuộn cho nó theo cách đó bạn có thể tái sử dụng kiểm soát.
liên kết này bị hỏng .... – patrickbadley
trên posted 2 năm trước ... – evanb
@evanb đó là chính xác tại sao câu trả lời có chứa gì, nhưng liên kết không được khuyến khích. –
- 1. Di chuyển uiimageview hoạt hình
- 2. Làm hình nền động android với di chuyển sai lệch
- 3. Cocos2d di chuyển hình ảnh động trên đường dẫn
- 4. Di chuyển mô hình đường ray sau khi di chuyển
- 5. Trung tâm hình học di chuyển Three.js
- 6. Cách: Tự động di chuyển Google Gauge?
- 7. C# Di chuyển qua một hình ảnh theo chuyển động xoắn ốc?
- 8. Di chuyển động của UIScrollView bằng CoreAnimation
- 9. Di chuyển hình chữ nhật với chuột
- 10. Tắt di chuyển trên thiết bị di động
- 11. jQuery di động nội dung trang dọc di chuyển dọc
- 12. winform di chuyển hình ảnh bên trong hình tượng
- 13. rake db: di chuyển không hoạt động
- 14. TFPT Unshelve di chuyển không hoạt động
- 15. Di động mảng định hình lại
- 16. Rails 3.1 động cơ di chuyển không hoạt động
- 17. Blackberry - Tự động di chuyển trình quản lý trường dọc
- 18. Chuyển tiếp trang di động jQuery CSS3 mà không cần thư viện di động jQuery
- 19. Di chuyển màn hình đến vị trí nhất định
- 20. Đường ray - Tạo tập lệnh di chuyển từ mô hình
- 21. Tôi phải làm gì để lấy Dữ liệu cốt lõi tự động di chuyển mô hình?
- 22. Làm thế nào để di chuyển một TextView động trên màn hình? (framelayout)
- 23. Di chuyển hoạt hình không trơn tru trong android
- 24. Làm mờ dần khi di chuyển hình ảnh bằng CSS3?
- 25. Di chuyển Django mô hình để hạn chế unique_together
- 26. Hộp đèn di động?
- 27. Alembic: Cách di chuyển loại tùy chỉnh trong mô hình?
- 28. javascript - bản đồ google như di chuyển hình ảnh
- 29. Tìm hiểu các biến cấu hình swipe di động jQuery
- 30. Công cụ di chuyển lược đồ tự động Oracle
Giá trị animation.to sẽ là gì nếu tôi muốn cuộn theo chiều ngang? – Cobold
Có thể có lỗi trong mã: 'scrollViewer.ScrollableHeight * ((double) index/itemsControl.Items.Count); ' phải là ' scrollViewer.ScrollableHeight * ((double) index/(itemsControl.Items.Count -1)); Ví dụ: , nếu danh sách chứa 12 phần tử và tôi muốn cuộn đến phần tử cuối cùng (chỉ số 11), kết quả phải trở thành 'scrollViewer.ScrollableHeight * 1' cũng vậy.) – sim1
@ sim1: Chưa xem xét điều này trong một thời gian dài vì vậy tôi chắc chắn rằng bạn đã đúng. Cảm ơn bạn đã cập nhật :) –