【问题标题】:UWP ListView ScrollViewer - Reset Zoom (Factor)UWP ListView ScrollViewer - 重置缩放(因子)
【发布时间】:2019-02-03 02:27:46
【问题描述】:

我的 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>

有没有办法将放大后的图像重置为原始大小?

感谢大家的帮助!

【问题讨论】:

    标签: listview scrollview uwp-xaml bitmapimage


    【解决方案1】:

    是的,正如@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;
        }
    }
    

    然后您可以在 UserControl 后面的代码中获取 ScrollViewer 对象并重置其 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);
    }
    

    【讨论】:

      【解决方案2】:

      如果您从滚动查看器中获得句柄,则可以使用 ScrollViewer.ChangeView() 设置图像的放大程度。

      【讨论】:

        猜你喜欢
        • 2017-06-13
        • 2018-03-09
        • 2016-05-20
        • 2015-07-18
        • 1970-01-01
        • 2019-05-31
        • 2013-03-13
        • 1970-01-01
        • 2020-05-15
        相关资源
        最近更新 更多