Metro style : Scrolling with mouse wheel

試著忘記壹切 提交于 2019-12-06 09:19:48

It is work GridView style. i remove scrollviewr property

<Style x:Key="GridViewInGridViewStyle" TargetType="GridView">
    <Setter Property="Padding" Value="0,0,0,10"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="TabNavigation" Value="Once"/>
    <Setter Property="IsSwipeEnabled" Value="True"/>
    <Setter Property="ItemContainerTransitions">
        <Setter.Value>
            <TransitionCollection>
                <AddDeleteThemeTransition/>
                <ContentThemeTransition/>
                <ReorderThemeTransition/>
                <EntranceThemeTransition IsStaggeringEnabled="False"/>
            </TransitionCollection>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapGrid Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="GridView">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <ItemsPresenter HeaderTemplate="{TemplateBinding HeaderTemplate}" Header="{TemplateBinding Header}" HeaderTransitions="{TemplateBinding HeaderTransitions}" Padding="{TemplateBinding Padding}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

If you re-template the GridView and remove the inner ScrollViewer, then mouse-wheel scrolling will work, but swipe-to-select will not work. If you want both, the trick is to use the AddHandler() method to add a handler for the PointerWheelChanged event and set the e.Handled property to false. That will allow the mouse wheel events to bubble up to your outer ScrollViewer properly:

public class CustomGridView : GridView
{
    protected override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var sv = this.GetTemplateChild("ScrollViewer") as UIElement;
        if (sv != null)
            sv.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(OnPointerWheelChanged), true);
    }

    private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs e)
    {
        e.Handled = false;
    }
}

I implemented this exact scenario and it works for me. Full details here: http://briandunnington.github.com/gridview-in-a-scrollviewer.html

UPDATE: sorry, I misread the question. If you place a GridView in a GridView, you do have nested ScrollViewers and you do need that code on the inner GridViews or mousewheel scrolling will not work.

However, why do you nest GridViews in a GridView?

Take a look at the grouping functionality built into winrt.

Alternatively, place the inner GridViews in a simple ItemsControl with a StackPanel with horizontal orientation as the ItemsPanel, and that ItemsControl in a ScrollViewer. If you do place multiple GridViews in a ScrollViewer (directly or indirectly), you do need that code to remove the ScrollViewer from in inner (i.e. nested) GridViews, or mousewheel scrolling will not work.

ORIGINAL ANSWER:

That code is needed only if you place the GridView in a ScrollViewer.

If the the GridView is the only thing to display, you don't need to place it in a ScrollViewer, and you don't need that code.

I'm thinking your real question is about how to layout GridView correctly, as the templates included in Visual Studio 11 beta (from consumer preview) do a really bad job there.

Try this:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid>
      <!-- Back button and page title go here -->
    </Grid>

    <GridView x:Name="itemsGridView" Grid.Row="1"
            AutomationProperties.AutomationId="ItemsGridView"
            AutomationProperties.Name="Items"
            ItemsSource="{Binding MyListOfSItems}"
            ItemTemplate="{StaticResource myItemTemplate}">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapGrid x:Name="itemGridViewPanel" Margin="116,53,116,46"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
    </GridView>

</Grid>

Now there is only one ScrollViewer, i.e. the one in the GridView, so there are no conflicts from two ScrollViewers nested in each other and the one ScrollViewer handles the mouse automatically.

In addition, the margin is correct, but when scrolling items are allowed to move into the margin area.

This scrolls fine for me:

<SemanticZoom>
    <SemanticZoom.ZoomedInView>
        <GridView>
            <GridView.ItemContainerStyle>
                <Style
                    TargetType="GridViewItem">
                    <Setter
                        Property="Width"
                        Value="250" />
                    <Setter
                        Property="Height"
                        Value="250" />
                    <Setter
                        Property="FontSize"
                        Value="32" />
                </Style>
            </GridView.ItemContainerStyle>
            <GridViewItem
                Content="Apple"/>
            <GridViewItem
                Content="Banana" />
            <GridViewItem
                Content="Cherry" />
            <GridViewItem
                Content="Donut" />
            <GridViewItem
                Content="Eggplant" />
            <GridViewItem
                Content="Fig" />
            <GridViewItem
                Content="Grape" />
            <GridViewItem
                Content="Ham" />
            <GridViewItem
                Content="Ice Cream" />
            <GridViewItem
                Content="Jam" />
            <GridViewItem
                Content="Kale" />
            <GridViewItem
                Content="Lemon" />
            <GridViewItem
                Content="Muffin" />
            <GridViewItem
                Content="Nut" />
            <GridViewItem
                Content="Orange" />
            <GridViewItem
                Content="Pear" />
            <GridViewItem
                Content="Raspberry" />
            <GridViewItem
                Content="Steak" />
            <GridViewItem
                Content="Turkey" />
            <GridViewItem
                Content="Udon" />
            <GridViewItem
                Content="Vodka" />
            <GridViewItem
                Content="Wine" />
            <GridViewItem
                Content="Xanthan Gum" />
            <GridViewItem
                Content="Yam" />
            <GridViewItem
                Content="Zucchini" />
        </GridView>
    </SemanticZoom.ZoomedInView>
    <SemanticZoom.ZoomedOutView>
        <GridView
            ItemsSource="ABCDEFGHIJKLMNOPQRSTUVWXYZ">
            <GridView.ItemContainerStyle>
                <Style
                    TargetType="GridViewItem">
                    <Setter
                        Property="Width"
                        Value="400" />
                    <Setter
                        Property="Height"
                        Value="100" />
                    <Setter
                        Property="FontSize"
                        Value="72" />
                </Style>
            </GridView.ItemContainerStyle>
        </GridView>
    </SemanticZoom.ZoomedOutView>
</SemanticZoom>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!