最近在做列表头部的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的问题的更多相关文章

  1. WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法

    最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性: 然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位 起初还以为是显卡的问题 ...

  2. 重新绑定ItemsSource先设置ItemsSource = null;的原因

    即报错信息为:在使用 ItemsSource 之前,项集合必须为空.   原因:Items和ItemSource,只能有一个生效,想用其中一个,另一个必须是空.   重新绑定ItemSource,虽然 ...

  3. win10 UWP FlipView

    FlipView 可以让用户逐个浏览的项目集合 <FlipView Grid.Row="0" Height="100" Margin="10,1 ...

  4. UWP 双向绑定,在ListView中有个TextBox,怎么获取Text的值

    要求:评论宝贝的时候一个订单里面包含多个产品,获取对产品的评论内容哦 1. xaml界面 <ListView x:Name="lvDetail"> <ListVi ...

  5. win10 uwp xaml 绑定接口

    本文告诉大家如何在 xaml 绑定属性使用显式继承接口 早上快乐 就在你的心问了我一个问题,他使用的属性是显式继承,但是无法在xaml绑定 我写了简单的代码,一个接口和属性 public class ...

  6. UWP ListView 绑定 单击 选中项 颜色

    refer: https://www.cnblogs.com/lonelyxmas/p/7650259.html using System; using System.Collections.Gene ...

  7. WPF/UWP 绑定中的 UpdateSourceTrigger

    在开发 markdown-mail 时遇到了一些诡异的情况.代码是这么写的: <TextBox Text="{Binding Text, Mode=TwoWay}"/> ...

  8. 聊聊大麦网UWP版的首页顶部图片联动效果的实现方法

    随着Windows10的发布,国内已经有越来越多的厂商上架了自家的通用应用程序客户端,比如QQ.微博.大麦等.所实话,他们设计的确实很好,很符合Windows10 的设计风格和产品理念,而对于开发者而 ...

  9. 在WPF中使用变通方法实现枚举类型的XAML绑定

    问题缘起 WPF的分层结构为编程带来了极大便利,XAML绑定是其最主要的特征.在使用绑定的过程中,大家都普遍的发现枚举成员的绑定是个问题.一般来说,枚举绑定多出现于与ComboBox配合的情况,此时我 ...

随机推荐

  1. C#执行外部程序之执行DOS命令和批处理

    在项目开发中,有时候要处理一些文件,比如视频格式的转换,如果用C开发一套算法,再用C#调用,未免得不偿失!有时候调用现有的程序反而更加方便.今天就来说一下C#中如何调用外部程序,执行一些特殊任务. 这 ...

  2. 一天一小段js代码(no.1)

    10000个数字中缺少三个数,编程找出缺少的三个数字. 算法实现: /*生成10000个数中随机抽掉三个数后的数组*/ function supplyRandomArray(){ /*生成含有1000 ...

  3. Linux 比较判断运算(if test)

    200 ? "200px" : this.width)!important;} --> 介绍 本篇文章主要是列举在shell命令中常出现的一些用来做比较的运算符,这些运算符是 ...

  4. Metro风格的Android界面应用

    最近项目中需要一个选择月份查询客户余额的功能,原先的android只能满足查询当月,不可以查询任意月份.当然改起来还是很简单的,服务端增加一个月份参数,客户端传入这个参数即可.闲来无事,月份的选择风格 ...

  5. Unity3D使用经验总结 缺点篇

    不论是从官方手册,还是各种第三方教程,几乎涉及到的,都是讲如何使用U3D,以及U3D的优点. 虽然我是用的一个让步语气,但请不要否认U3D的这些优点,它们的确存在. 但对于一个引擎的特性来说,优点与缺 ...

  6. 浅析SQL Server实现分布式事务的两阶段提交协议2PC

    不久之前团队有个新人问我一个很重要的web服务接口如何保证事务的问题.因为涉及到跨库事务,当时我只是回答目前我们的SOA框架都不支持跨库事务.然后就问到了数据库跨库事务是如何实现的,我只能凭印象含糊回 ...

  7. VS 2008 生成操作中各个选项的差别

    近日,在编译C#项目时经常发现有些时候明明代码没错,但就是编译不过,只有选择重新编译或者清理再编译才会不出错,本着求学的态度,搜罗了下VS2008IDE中生成操作的种类以及差别,整理如下:   内容( ...

  8. iOS给图片添加滤镜&使用openGLES动态渲染图片

    给图片增加滤镜有这两种方式: CoreImage / openGLES 下面先说明如何使用CoreImage给图片添加滤镜, 主要为以下步骤: #1.导入CIImage格式的原始图片 #2.创建CIF ...

  9. 日志分析系统——Hangout源码学习

    这两天看了下hangout的代码,虽然没有运行体验过,但是也算是学习了一点皮毛. 架构浅谈 Hangout可以说是java版的Logstash,我是没有测试过性能,不过据说是kafka这边性能要高出L ...

  10. Atitit Atitit 图像处理之  Oilpaint油画滤镜 水彩画 源码实现

    Atitit Atitit 图像处理之 Oilpaint油画滤镜 水彩画 源码实现 1.1. 具体原理参考1 2. 水彩画滤镜算法如下:1 2.1. 这个其实就是灰度层次降低维度的过程.2 2.2. ...