UWP ListView ScrollViewer-重置缩放(因子)

时间:2018-08-28 12:37:52

标签: listview scrollview uwp-xaml bitmapimage

我的ListView拥有BitmapImages,我想允许放大这些图像。

如果我使用 ScrollViewer。。。。我没有放大的因素。

<UserControl
x:Class="SimplePdfViewer.SimplePdfViewerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SimplePdfViewer"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Unloaded="root_Unloaded"
x:Name="root">
<UserControl.Resources>
    <Style TargetType="ListViewItem" x:Key="ListViewItemEdit">
        <!-- spacing between pages-->
        <Setter Property="Margin" Value="0 0 0 3"/>
        <!-- Setting Background Color of PDf Placeholders to White-->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListViewItem">
                    <ListViewItemPresenter ContentTransitions="{TemplateBinding ContentTransitions}"
                                PlaceholderBackground="White"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>
<Grid>
    <ListView x:Name="PdfListView"
              ItemsSource="{x:Bind DocumentDataSource}"
              ScrollViewer.ZoomMode="Enabled" 
              ItemContainerStyle="{StaticResource ListViewItemEdit}"
              SelectionMode="None">

        <ListView.ItemTemplate>
            <!-- Implement Dynamic Width! -> Added for Placeholder Width -->
            <DataTemplate x:DataType="BitmapImage">
                <ListViewItem 
                    Height="1180"
                    Width="800"
                    Background="White"
                    IsHitTestVisible="False">
                    <Image Source="{x:Bind}"/>
                </ListViewItem>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

是否可以将放大的图像重置为原始大小?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

如果您从滚动查看器获得了句柄,则可以使用ScrollViewer.ChangeView()来设置图像的缩放比例。

答案 1 :(得分:0)

是的,作为@ user1419778的答案,您应该从ListView中找到ScrollViewer对象,然后可以使用ScrollViewer.ChangeView方法的zoomFactor参数重置其ZoomFactor

首先,您可以使用辅助程序类中的以下方法从ListView中找到ScrollViewer对象。

public static class UIHelper
{
    public static ChildElement FindVisualChild<ChildElement>(this DependencyObject obj)
        where ChildElement : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is ChildElement)
                return (ChildElement)child;
            else
            {
                ChildElement childOfChild = FindVisualChild<ChildElement>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }
}

然后,您可以获取ScrollViewer对象,并在后面的UserControl代码中重置其ZoomFactor

private void ResetButton_Click(object sender, RoutedEventArgs e)
{
    var scrollViewer=PdfListView.FindVisualChild<ScrollViewer>();
    //change view to vertical offset and set the zoom factor to 1.
    //The default zoom factor is 1.0, where 1.0 indicates no additional scaling
    scrollViewer.ChangeView(0, scrollViewer.VerticalOffset, 1.0f);
}