【问题标题】:Get flipview ItemsPresenter to show next and previous items获取 Flipview ItemsPresenter 以显示下一个和上一个项目
【发布时间】:2016-10-24 22:38:14
【问题描述】:

我有一个 uwp 翻转视图,想要让它显示一些下一个和上一个元素。本质上,这意味着使每个翻转视图项稍微小一些,以便您知道翻转视图可以滚动。这是我目前拥有的屏幕截图:

翻转视图是带有迈克尔·法斯宾德圆形图像的部分。你可以滑动到下一张,所以我想在左边显示下一张图片。我注意到 FlipView 默认样式在滚动查看器中有一个 ItemsPresenter(我们可以通过在 Visual Studio/Edit Style/Edit a Copy ... 中右键单击它来修改 FlipView 默认样式)。如果我将该 ItemsPresenter 的边距设置为每边的 -100,它在某些时候可以工作,但行为是奇怪的、不可预测的,并且取决于窗口的宽度。还有什么解决办法?

【问题讨论】:

    标签: c# xaml uwp flipview itemspresenter


    【解决方案1】:

    如果我将 ItemsPresenter 的边距设置为每边 -100,它在某些点上有效,但行为很奇怪,不可预测,并且取决于窗口的宽度。还有什么解决办法?

    设置MarginMargin 是对的,但我认为-100 的值不能解决这里的问题,它应该取决于FilpView 的宽度,而不是窗口的宽度宽度。在这里我有一个解决方案还设置了Margin 属性:

    <ItemsPresenter Margin="{Binding Width, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource marginvct}}" />
    

    而我的转换器是这样的:

    public class MarginConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var width = (double)value;
            double itemmargin = width / 6;
            Thickness margin = new Thickness(itemmargin, 0, itemmargin, 0);
            return margin;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    

    从这段代码中可以看出,我将每个项目的宽度设置为整个FlipView的宽度的2/3,并保持左右两侧有一个空间与FlipView的1/6的宽度,所以下一个、当前和上一个元素会一起显示。

    并且由于我将Margin的值绑定到Width,所以现在使用FlipView时,应该设置Width属性。你说这取决于窗口的大小,我猜你希望你的FlipView的宽度等于窗口的宽度,所以例如你可以这样编码:

    <FlipView ItemsSource="{x:Bind collection}" Style="{StaticResource FlipViewStyle}" Width="{x:Bind CustomWidth}">
        <FlipView.ItemTemplate>
            <DataTemplate>
                <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
                    <Grid.Background>
                        <ImageBrush ImageSource="{Binding ContactImage}" />
                    </Grid.Background>
                    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                        <Ellipse Width="200" Height="200">
                            <Ellipse.Fill>
                                <ImageBrush ImageSource="{Binding ContactImage}" />
                            </Ellipse.Fill>
                        </Ellipse>
                        <TextBlock Margin="0,30,0,0" Text="{Binding ContactName}" FontSize="25" FontWeight="Bold" />
                        <TextBlock Margin="0,15,0,0" Text="{Binding ContactNumber}" FontSize="20" />
                        <TextBlock Margin="0,15,0,0" Text="{Binding ContactAddress}" FontSize="20" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </FlipView.ItemTemplate>
    </FlipView>
    

    CustomWidth 后面的代码是这样的:

    private double CustomWidth;
    
    public MainPage()
    {
        this.InitializeComponent();
        CustomWidth = Window.Current.Bounds.Width;
    }
    

    这是这个演示的渲染图:

    【讨论】:

    • 它似乎仍然对大小变化做出奇怪的反应:/ 我在 Page 的 SizeChanged 事件中将翻转视图的大小设置为窗口的宽度。我也得到了边距不总是相同宽度的行为:
    • @user2950509,我无法重现您的问题,我在这里没有发现任何奇怪的东西。但是如果在page的SizeChanged事件中设置宽度为flipview,则需要在这里实现INotifypropertyChanged来通知FlipView改变它的宽度,并且首先给宽度赋一个初始值也很重要,否则会出错。你在上一个问题中检查了我的回答吗?
    • 这是我得到的行为:imgur.com/vGEqdAo。如您所见,这些项目有时也会消失。
    • 关于宽度,我所做的就是这样设置:flipview.Width = Window.Current.Bounds.Width;在页面的 SizeChanged 事件上:/
    • @user2950509,我认为在更改窗口大小期间项目会消失是正常的,因为在此期间边距值将始终重新计算。我仍然无法通过 SizeChanged 重现您的边距问题,您是如何设置边距的?
    猜你喜欢
    • 2015-01-12
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多