WindowsPhone8中LongListSelector的扩展解决其不能绑定SelectdeItem的问题
2014-10-28 14:43
316 查看
微软在Wp8中集成了LongListSelector, 但是该控件在ViewModel中不能实现的SelectdeItem双向绑定,因为其不是DependencyProperty没办法只能实现扩展!
1.实现LongListSelector的扩展ExtendedSelector
2.在xmal中使用 扩展ExtendedSelector
a.定义数据模板
b.定义LongListSelector的Style
c.Page页面代码的实现
实现效果:
1.实现LongListSelector的扩展ExtendedSelector
public enum PositionOnAdd { Top, Default, NewItem } public class ExtendedSelector : LongListSelector { public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExtendedSelector), new PropertyMetadata(default(object))); public static readonly DependencyProperty SelectionModeProperty = DependencyProperty.Register("SelectionMode", typeof(SelectionMode), typeof(ExtendedSelector), new PropertyMetadata(SelectionMode.Single)); public static readonly DependencyProperty RepositionOnAddStyleProperty = DependencyProperty.Register("RepositionOnAddStyle", typeof(PositionOnAdd), typeof(ExtendedSelector), new PropertyMetadata(PositionOnAdd.Default)); public PositionOnAdd RepositionOnAddStyle { get { return (PositionOnAdd)GetValue(RepositionOnAddStyleProperty); } set { SetValue(RepositionOnAddStyleProperty, value); } } public SelectionMode SelectionMode { get { return (SelectionMode)GetValue(SelectionModeProperty); } set { SetValue(SelectionModeProperty, value); } } public new object SelectedItem { get { return GetValue(SelectedItemProperty); } set { SetValue(SelectedItemProperty, value); } } public ExtendedSelector() { SelectionChanged += (sender, args) => { if (SelectionMode == SelectionMode.Single) SelectedItem = args.AddedItems[0]; else if (SelectionMode == SelectionMode.Multiple) { if (SelectedItem == null) { SelectedItem = new List<object>(); } foreach (var item in args.AddedItems) { ((List<object>)SelectedItem).Add(item); } foreach (var removedItem in args.RemovedItems) { if (((List<object>)SelectedItem).Contains(removedItem)) { ((List<object>)SelectedItem).Remove(removedItem); } } } }; Loaded += (sender, args) => { ((INotifyCollectionChanged)ItemsSource).CollectionChanged += (sender2, args2) => { if (ItemsSource.Count > 0 && args2.NewItems != null) { switch (RepositionOnAddStyle) { case PositionOnAdd.NewItem: int index = ItemsSource.IndexOf(args2.NewItems[0]); if (index >= 0) ScrollTo(ItemsSource[index]); break; case PositionOnAdd.Top: ScrollTo(ItemsSource[0]); break; } } }; }; } }
2.在xmal中使用 扩展ExtendedSelector
a.定义数据模板
<DataTemplate x:Name="LLSDataTemplate"> <ListBoxItem Margin="0,0,0,6"> <StackPanel> <StackPanel Margin="0,0,0,12"> <TextBlock toolkit:SlideInEffect.LineIndex="1" Foreground="{StaticResource FontGroundThemeBrush}" TextWrapping="Wrap" FontSize="30" TextTrimming="None" Text="{Binding name}" /> <TextBlock toolkit:SlideInEffect.LineIndex="2" Foreground="{StaticResource FontGroundThemeBrush}" Opacity="0.7" TextWrapping="Wrap" TextTrimming="None" FontSize="18" Text="{Binding content}" /> </StackPanel> </StackPanel> </ListBoxItem> </DataTemplate>
b.定义LongListSelector的Style
<Style x:Key="JumpListStyle" TargetType="phone:LongListSelector"> <Setter Property="GridCellSize" Value="111,111"/> <Setter Property="LayoutMode" Value="Grid" /> <Setter Property="Margin" Value="18,12,0,0"/> <Setter Property="ItemTemplate"> <Setter.Value> <DataTemplate> <Border Background="{Binding Converter={StaticResource BackgroundConverter}}" Margin="6" > <TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Padding="11,0,0,1" Foreground="{Binding Converter={StaticResource ForegroundConverter}}" VerticalAlignment="Bottom" /> </Border> </DataTemplate> </Setter.Value> </Setter> </Style> <DataTemplate x:Key="GroupHeaderTemplate"> <Border Background="Transparent" Padding="5"> <Border Background="{StaticResource BackgroundThemeBrush}" BorderBrush="{StaticResource BackgroundThemeBrush}" Width="62" Height="62" Margin="-6,0,18,6" HorizontalAlignment="Left"> <TextBlock Text="{Binding Key}" Foreground="{StaticResource PhoneForegroundBrush}" FontSize="48" Padding="6,6,6,11" FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/> </Border> </Border> </DataTemplate>
c.Page页面代码的实现
<control:ExtendedSelector Margin="12,0,12,24" x:Name="ReaultListBox" ItemsSource="{Binding SearchReaultItem, Mode=TwoWay}" SelectedItem="{Binding SelectCydqItem,Mode=TwoWay}" GroupHeaderTemplate="{StaticResource GroupHeaderTemplate}" HideEmptyGroups="True" IsGroupingEnabled="True" ItemTemplate="{StaticResource LLSDataTemplate}" toolkit:TurnstileFeatherEffect.FeatheringIndex="1" JumpListStyle="{StaticResource JumpListStyle}" RepositionOnAddStyle="Top" > <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <cmd:EventToCommand Command="{Binding SearchVM.SelectionChangedCommand,Source={StaticResource Locator}}" CommandParameter="{Binding Path=SelectedItem,ElementName=ReaultListBox}"/> </i:EventTrigger> </i:Interaction.Triggers> </control:ExtendedSelector>
实现效果:
说明: RepositionOnAddStyle="Top" 有Top,Default,NewItem ,Top 为加载完数据后展示从头部开始 NewItem为加载完数据后展示从尾部开始
相关文章推荐
- 如何使用 BindingSource 绑定 ListBox,同时解决绑定 List<T> 后修改数据源不能同时刷新界面显示的问题
- 临时对象不能被绑定到非const引用参数上====》扩展到临时对象问题
- 章鱼哥出品—VB.NET DataGridView绑定数据源 "与货币管理器的位置关联的行不能设置为不可见" 问题的解决
- C#中DataGridView绑定了DataTable后,通过代码修改DataGridView中的数据,总有一行(被修改过并被用户选中的行集合中索引为0的行)不能被UpDate回数据库的问题和解决办法
- 解决docker不能绑定静态的外网固定ip的问题
- 解决DataGridView绑定List后不能排序的问题
- 本人扩展的可绑定Dataset的Treeview,遇到不能展开的问题
- 解决win7有些硬盘不能扩展卷的问题
- 如何解决程序退出重启后不能绑定端口的问题?
- 解决谷歌为Android系统提供的支持库android-support-v4。jar不能绑定查看源码的问题
- 解决DataGridView绑定List后不能排序的问题
- 在WindowsPhone8中生成基于MVVM Light的LongListSelector命令绑定
- jQuery在异步请求数据返回后,调用$("selector").html(data.content);之后因为一些特殊字符或者',"不能显示内容的问题解决办法
- spring mvc绑定对象String转Date解决入参不能是Date的问题
- 解决一个在国内不能安装chrome扩展的问题
- myEclipse/eclipse代码提示、模板提示功能扩展与eclipse代码不能提示问题解决完整版
- vs2008使用过AnkhSVN后不能绑定到vss的问题解决
- ubuntu单网卡绑定多IP (解决ubuntu下用局域网时不能连接到外网问题)
- 密码框密码不能绑定问题解决方案
- Linux下tomcat作为守护进程运行(开机启动、以指定的用户运行、解决非root身份不能绑定1024以下端口的问题)的配置方法