Скрипт .xaml
Код: Выделить всё
Код: Выделить всё
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
namespace TestAPP
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Scroll up when up arrow is clicked
private void UpArrow_Click(object sender, RoutedEventArgs e)
{
MyScrollViewer.LineUp(); // Scroll one line up
}
// Scroll down when down arrow is clicked
private void DownArrow_Click(object sender, RoutedEventArgs e)
{
MyScrollViewer.LineDown(); // Scroll one line down
}
// Handle scroll changes (you can manually adjust the thumb size or handle any visual logic here)
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
// Optional: Add any additional logic to manage thumb movement or other scroll events.
}
}
public class ScrollThumbConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 3 || !(values[0] is double verticalOffset) || !(values[1] is double scrollableHeight) || !(values[2] is double viewportHeight))
{
return new TranslateTransform(0, 0); // Default transform
}
// Calculate the thumb's position
double trackHeight = scrollableHeight + viewportHeight; // Total track height
double ratio = scrollableHeight > 0 ? verticalOffset / scrollableHeight : 0;
double thumbPosition = ratio * (trackHeight - viewportHeight); // Calculate thumb position
return new TranslateTransform(0, thumbPosition);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class ThumbHeightConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2 || !(values[0] is double viewportHeight) || !(values[1] is double scrollableHeight))
{
return 50.0; // Default thumb height if values are missing
}
// Calculate the thumb height as a ratio of the viewport to the total scrollable height
double totalHeight = scrollableHeight + viewportHeight;
double thumbHeight = (viewportHeight / totalHeight) * viewportHeight;
return Math.Max(20, thumbHeight); // Ensure a minimum thumb height for usability
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... png-in-wpf