【UWP】FlipView绑定ItemsSource,Selectedindex的问题
最近在做列表头部的Carousel展示,Carousel使用的是FlipView展示,另外使用ListBox显示当前页,如下图

我们先设置一个绑定的数据源
public class GlobalResource : INotifyPropertyChanged
{
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get
{
return _items = _items ?? new ObservableCollection<string>
{
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
Guid.NewGuid().ToString(),
};
}
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
} public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Items作为数据源绑定在FlipView和ListBox上,布局代码如下
<Page x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:App1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Page.Resources>
<local:GlobalResource x:Key="GlobalResource" />
<Style x:Key="DotListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Background" Value="Transparent" />
<Setter Property="TabNavigation" Value="Local" />
<Setter Property="Padding" Value="12,11,12,13" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Margin" Value="5" />
<Setter Property="UseSystemFocusVisuals" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="LayoutRoot"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled" />
<VisualState x:Name="PointerOver" />
<VisualState x:Name="Pressed" />
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="dotEllipse" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="dotEllipse" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedPointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="dotEllipse" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedPressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="dotEllipse" Storyboard.TargetProperty="Fill">
<DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="dotEllipse"
Width="10"
Height="10"
Control.IsTemplateFocusTarget="True"
Fill="White" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView>
<ListView.Header>
<Grid Height="200">
<FlipView x:Name="flipView" ItemsSource="{Binding Source={StaticResource GlobalResource}, Path=Items}">
<FlipView.ItemTemplate>
<DataTemplate>
<Rectangle Height="200"
Margin="5,0"
Fill="Red" />
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView> <ListBox x:Name="listBox"
Margin="0,0,0,8"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Background="Transparent"
ItemContainerStyle="{StaticResource DotListBoxItemStyle}"
ItemsSource="{Binding Source={StaticResource GlobalResource},
Path=Items}"
SelectedIndex="{Binding ElementName=flipView,
Path=SelectedIndex,
Mode=TwoWay}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel> <ListBox.ItemTemplate>
<DataTemplate>
<Ellipse Width="10"
Height="10"
Fill="White" />
</DataTemplate>
</ListBox.ItemTemplate> </ListBox>
</Grid> </ListView.Header>
<Button Click="ButtonBase_OnClick">test</Button>
</ListView>
</Grid>
</Page>
MainPage.xaml
一切正常显示

问题:
下面我们需要修改数据源
var globalResource = (GlobalResource) Resources["GlobalResource"];
globalResource.Items.Clear();
for (var i = ; i < ; i++)
{
globalResource.Items.Add(Guid.NewGuid().ToString());
} Debug.WriteLine("flipView.SelectedIndex = {0}", flipView.SelectedIndex);
Debug.WriteLine("listBox.SelectedIndex = {0}", listBox.SelectedIndex);

虽然数据源变了,但是并没有选中当前页(第一个点不为蓝色),通过输出信息发现SelectedIndex都是0,并没有改变
跟踪发现,调用ObservableCollection.Clear方法的时候SelectedIndex都被设为了-1,Add第一个的时候SelectedIndex被置为0,数据源和相关数据都改变了,不知道为什么样式没有出发(VisualState)由于不知道ListView内部实现,我们无法得知具体原因是啥
解决:
对于上面问题,可以通过下面方式解决
重新改变SelectedIndex让ListBox更新样式
var globalResource = (GlobalResource) Resources["GlobalResource"];
globalResource.Items.Clear();
for (var i = ; i < ; i++)
{
globalResource.Items.Add(Guid.NewGuid().ToString());
} flipView.SelectedIndex = -;
flipView.SelectedIndex = ;
Demo:
http://files.cnblogs.com/files/bomo/CarouselDemo.zip
【UWP】FlipView绑定ItemsSource,Selectedindex的问题的更多相关文章
- WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法
最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性: 然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位 起初还以为是显卡的问题 ...
- 重新绑定ItemsSource先设置ItemsSource = null;的原因
即报错信息为:在使用 ItemsSource 之前,项集合必须为空. 原因:Items和ItemSource,只能有一个生效,想用其中一个,另一个必须是空. 重新绑定ItemSource,虽然 ...
- win10 UWP FlipView
FlipView 可以让用户逐个浏览的项目集合 <FlipView Grid.Row="0" Height="100" Margin="10,1 ...
- UWP 双向绑定,在ListView中有个TextBox,怎么获取Text的值
要求:评论宝贝的时候一个订单里面包含多个产品,获取对产品的评论内容哦 1. xaml界面 <ListView x:Name="lvDetail"> <ListVi ...
- win10 uwp xaml 绑定接口
本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...
- UWP ListView 绑定 单击 选中项 颜色
refer: https://www.cnblogs.com/lonelyxmas/p/7650259.html using System; using System.Collections.Gene ...
- WPF/UWP 绑定中的 UpdateSourceTrigger
在开发 markdown-mail 时遇到了一些诡异的情况.代码是这么写的: <TextBox Text="{Binding Text, Mode=TwoWay}"/> ...
- 聊聊大麦网UWP版的首页顶部图片联动效果的实现方法
随着Windows10的发布,国内已经有越来越多的厂商上架了自家的通用应用程序客户端,比如QQ.微博.大麦等.所实话,他们设计的确实很好,很符合Windows10 的设计风格和产品理念,而对于开发者而 ...
- 在WPF中使用变通方法实现枚举类型的XAML绑定
问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...
随机推荐
- 备忘: Install MODI for use with Microsoft Office 201x
简介 Microsoft Office 2010 中删除了 Microsoft Office Document Imaging (MODI).本文提供了在计算机上安装 MODI 的步骤,还说明了重新获 ...
- C# 关于Try/Catch对系统性能影响的总结
自从开始考虑代码的运行效率和性能以后,写代码考虑的东西越来越多了,比如什么时候应该加try/catch?加太多的try/catch会不会降低性能?今天就来分享一下对try/catch对性能影响的一些看 ...
- 【译】用jQuery 处理XML--浏览器中的XML与JavaScript
用jQuery 处理XML--写在前面的话 用jQuery 处理XML-- DOM(文本对象模型)简介 用jQuery 处理XML--浏览器中的XML与JavaScript 用jQuery 处理XML ...
- Yii 框架学习--02 进阶
应用结构 入口文件 文件位置: web/index.php <?php //开启debug,应用会保留更多日志信息,如果抛出异常,会显示详细的错误调用堆栈 defined('YII_DEBUG' ...
- Nodejs·内存控制
之前有考虑过Node中的内存管理,但是没想到Node的内存机制与JVM如此相像. 看完这部分的内容,基本可以了解Node中的内存使用技巧: 1 尽量不要做过多的缓存 2 使用队列应该有限制 3 注意全 ...
- 模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能
Login <%@ page language="java" contentType="text/html; charset=UTF-8" pageEnc ...
- 选课数据库SQL语句练习题
表(一)Student (学生表) 属性名 数据类型 可否为空 含 义 Sno varchar (20) 否 学号(主码) Sname varchar (20) 否 学生姓名 Ssex varchar ...
- 用 flow.ci 让 Hexo 持续部署
编者按:感谢 @小小小杜 投稿,原文链接Juglans' Blog.如果你也想体验 flow.ci 的自动化持续部署,来 http://flow.ci 首页提交申请,邀请码随后会发送到邮箱:) flo ...
- require.js笔记
笔记参考来源:阮一峰 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 1. 浏览器端的模块只能采用“异步加载”方式 = ...
- SQL*Loader之CASE2
CASE2 1. 控制文件 [oracle@node3 ulcase]$ cat ulcase2.ctl -- NAME -- ulcase2.ctl - SQL*Loader Case Study ...