I have a GridView
in a ListView
. I want to add a Ctrl+MWheelUp zoom to the contents.
I've achieved the zoom part using a ScaleTransform
with the below code, however, as this is applied to the ListView
as a whole, it also scales the scroll bars. Ideally, I'd like scrollbars to remain a fixed size (although obviously adjusting to the change in inner-content) - however, I'm not sure how I could achieve this. Would the only way to be to apply the ScaleTransform
to every child of every GridViewColumn
, or is there another method I could use to apply it to the ListView
as a whole, without also scaling the scroll bars?
My (simplified) xaml:
<ListView ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto"
x:Name="listView">
<ListView.View>
<GridView>
<GridViewColumn>...</GridViewColumn>
<GridViewColumn>...</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
xaml.cs:
public Control()
{
InitializeComponent();
var mouseWheelZoom = new MouseWheelZoom(listView);
PreviewMouseWheel += mouseWheelZoom.Zoom;
}
MouseWheelZoom
public class MouseWheelZoom
{
private readonly FrameworkElement _element;
private double _currentZoomFactor;
public MouseWheelZoom(FrameworkElement element)
{
_element = element;
_currentZoomFactor = 1.0;
}
public void Zoom(object sender, MouseWheelEventArgs e)
{
var handle = (Keyboard.Modifiers & ModifierKeys.Control) > 0;
if (!handle)
return;
ApplyZoom(e.Delta);
}
private void ApplyZoom(int delta)
{
var zoomScale = delta / 500.0;
var newZoomFactor = _currentZoomFactor += zoomScale;
_element.LayoutTransform = new ScaleTransform(newZoomFactor, newZoomFactor);
_currentZoomFactor = newZoomFactor;
}
}
You could use the always handy
FindChild<T>
function to retrieve theScrollContentPresenter
inside theListView
, and use your zooming function with it.Note that I've put the code inside the Loaded event handler. That's because the
ScrollContentPresenter
is part of theListView
Template and not a direct part of your view, so it won't exist until the control is fully loaded with its Styles and Templates.PD.: Also worth noting that some other parts of the
ListView
, like headers and such, won't be zoomed. Only the items will.