[UWP] FlipView绑定 [英] [UWP] FlipView Binding

查看:58
本文介绍了[UWP] FlipView绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FlipView中绑定SelectedItem时遇到一个小问题。

I'm having a small problem with binding SelectedItem in a FlipView.

似乎只有在翻转FV后才能使用SelectedItem,所以如果用户点击在第一项上我收到错误。

It appears that SelectedItem is only available after the FV has been flipped, so if the user clicks on the first item I get an error.

如何在不诉诸代码的情况下解决此问题?

How can I resolve this without resorting to code behind ?

TIA

推荐答案

Hello TIA,

Hello TIA,

我没有诉诸我的名单但是我不太确定你的意思是说"诉诸代码"。我尝试的内容如下:

I didn't resort my list but I'm not so sure what you mean by saying "resorting to code behind". What I tried is like the following:

UI代码:

        <FlipView SelectionChanged="flipView1_SelectionChanged" SelectedItem="{Binding MySelectedItem,Mode=TwoWay}" x:Name="flipView1" Width="480" Height="270" 
          BorderBrush="Black" BorderThickness="1">
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Image Width="480" Height="270" Stretch="UniformToFill"
                       Source="{Binding ImageUrl}"/>
                        <Border Background="#A5000000" Height="80" VerticalAlignment="Bottom">
                            <TextBlock Text="{Binding Name}" 
                               FontFamily="Segoe UI" FontSize="26.667" 
                               Foreground="#CCFFFFFF" Padding="15,20"/>
                        </Border>
                    </Grid>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>

代码背后:


using FlipViewSelectedItemBinding.ViewModel;
using System.Diagnostics;
using FlipViewSelectedItemBinding.Model;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace FlipViewSelectedItemBinding
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        MainVM mainvm { get; set; }
        public MainPage()
        {
            this.InitializeComponent();
            mainvm = new MainVM();
            flipView1.ItemsSource = mainvm.MyImageList;
            this.DataContext = mainvm;
            flipView1.SelectedItem = mainvm.MyImageList.FirstOrDefault();

        }

        private void flipView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mainvm.MySelectedItem != null)
            {
                var item = mainvm.MySelectedItem;
                Debug.WriteLine("Selected image's name is" + ((ImageListItem)item).Name);
            }

        }
    }
}

首先,我将在初始化页面时设置selecteditem。然后我将确保selecteditem不为null。通过这种方式,我将确保选定项目绑定并且用户不会收到任何错误。 

First, I will set the selecteditem when I initialize my page. Then I will make sure that the selecteditem is not null. In this way, I will make sure that the selecteditem is binded and the user will not get any error. 

祝你好运,

巴里


这篇关于[UWP] FlipView绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆