ListView 视图(View)
自定义视图,设置默认ListView,ListViewItems默认样式
public class VirtualStackPanelView : ViewBase
{
public static readonly DependencyProperty OrientationProperty =
VirtualStackPanel.OrientationProperty.AddOwner(typeof(VirtualStackPanelView)); public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
} public static readonly DependencyProperty SmallChangesProperty =
VirtualStackPanel.SmallChangesProperty.AddOwner(typeof(VirtualStackPanelView)); public uint SmallChanges
{
get { return (uint)GetValue(SmallChangesProperty); }
set { SetValue(SmallChangesProperty, value); }
} public static readonly DependencyProperty ItemContainerStyleProperty =
ItemsControl.ItemContainerStyleProperty.AddOwner(typeof(VirtualStackPanelView)); public Style ItemContainerStyle
{
get { return (Style)GetValue(ItemContainerStyleProperty); }
set { SetValue(ItemContainerStyleProperty, value); }
} public static readonly DependencyProperty ItemTemplateProperty =
ItemsControl.ItemTemplateProperty.AddOwner(typeof(VirtualStackPanelView)); public DataTemplate ItemTemplate
{
get { return (DataTemplate)GetValue(ItemTemplateProperty); }
set { SetValue(ItemTemplateProperty, value); }
} public static readonly DependencyProperty ItemWidthProperty =
VirtualStackPanel.ItemWidthProperty.AddOwner(typeof(VirtualStackPanelView)); public double ItemWidth
{
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
} public static readonly DependencyProperty ItemHeightProperty =
VirtualStackPanel.ItemHeightProperty.AddOwner(typeof(VirtualStackPanelView)); public double ItemHeight
{
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
} public static readonly DependencyProperty HorizontalContentAlignmentProperty =
StackPanel.HorizontalAlignmentProperty.AddOwner(typeof(VirtualStackPanelView)); public HorizontalAlignment HorizontalContentAlignment
{
get { return (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); }
set { SetValue(HorizontalContentAlignmentProperty, value); }
} public static readonly DependencyProperty CacheItemCountProperty =
DependencyProperty.Register("CacheItemCount", typeof(int),
typeof(VirtualStackPanelView), new UIPropertyMetadata(0)); public int CacheItemCount
{
get { return (int)GetValue(CacheItemCountProperty); }
set { SetValue(CacheItemCountProperty, value); }
} private GridViewColumnCollection _columns = new GridViewColumnCollection();
public GridViewColumnCollection Columns
{
get { return _columns; }
} public static readonly DependencyProperty ColumnHeaderContainerStyleProperty =
GridView.ColumnHeaderContainerStyleProperty.AddOwner(typeof(VirtualStackPanelView));
public Style ColumnHeaderContainerStyle
{
get { return (Style)GetValue(ColumnHeaderContainerStyleProperty); }
set { SetValue(ColumnHeaderContainerStyleProperty, value); }
} public static readonly DependencyProperty ColumnHeaderTemplateSelectorProperty =
GridView.ColumnHeaderTemplateSelectorProperty.AddOwner(typeof(VirtualStackPanelView));
public static readonly DependencyProperty ColumnHeaderStringFormatProperty =
GridView.ColumnHeaderStringFormatProperty.AddOwner(typeof(VirtualStackPanelView));
public static readonly DependencyProperty AllowsColumnReorderProperty =
GridView.AllowsColumnReorderProperty.AddOwner(typeof(VirtualStackPanelView));
public static readonly DependencyProperty ColumnHeaderContextMenuProperty =
GridView.ColumnHeaderContextMenuProperty.AddOwner(typeof(VirtualStackPanelView));
public static readonly DependencyProperty ColumnHeaderToolTipProperty =
GridView.ColumnHeaderToolTipProperty.AddOwner(typeof(VirtualStackPanelView)); protected override object DefaultStyleKey
{
get
{
return new ComponentResourceKey(GetType(), "virtualStackPanelViewDSK");
}
} protected override object ItemContainerDefaultStyleKey
{
get
{
return new ComponentResourceKey(GetType(), "virtualStackPanelViewItemDSK");
}
}
}
查看ListView源码发现,当视图改变的时候,用当前视图的DefaultStyleKey,ItemContainerDefaultStyleKey设置ListView的默认样式和项容器样式,对于默认视图和我们自定义的视图
public class ListView : ListBox
{
public static readonly DependencyProperty ViewProperty;
private ViewBase _previousView;
public ViewBase View
{
get
{
return (ViewBase)base.GetValue(ListView.ViewProperty);
}
set
{
base.SetValue(ListView.ViewProperty, value);
}
}
static ListView()
{
ListView.ViewProperty = DependencyProperty.Register("View", typeof(ViewBase), typeof(ListView), new PropertyMetadata(new PropertyChangedCallback(ListView.OnViewChanged)));
ListBox.SelectionModeProperty.OverrideMetadata(typeof(ListView), new FrameworkPropertyMetadata(SelectionMode.Extended));
}
private static void OnViewChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ListView listView = (ListView)d;
ViewBase viewBase = (ViewBase)e.OldValue;
ViewBase viewBase2 = (ViewBase)e.NewValue;
if (viewBase2 != null)
{
if (viewBase2.IsUsed)
{
throw new InvalidOperationException(SR.Get("ListView_ViewCannotBeShared"));
}
viewBase2.IsUsed = true;
}
listView._previousView = viewBase;
listView.ApplyNewView();
listView._previousView = viewBase2;
ListViewAutomationPeer listViewAutomationPeer = UIElementAutomationPeer.FromElement(listView) as ListViewAutomationPeer;
if (listViewAutomationPeer != null)
{
if (listViewAutomationPeer.ViewAutomationPeer != null)
{
listViewAutomationPeer.ViewAutomationPeer.ViewDetached();
}
if (viewBase2 != null)
{
listViewAutomationPeer.ViewAutomationPeer = viewBase2.GetAutomationPeer(listView);
}
else
{
listViewAutomationPeer.ViewAutomationPeer = null;
}
listViewAutomationPeer.InvalidatePeer();
}
if (viewBase != null)
{
viewBase.IsUsed = false;
}
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
ListViewItem listViewItem = element as ListViewItem;
if (listViewItem != null)
{
ViewBase view = this.View;
if (view != null)
{
listViewItem.SetDefaultStyleKey(view.ItemContainerDefaultStyleKey);
view.PrepareItem(listViewItem);
return;
}
listViewItem.ClearDefaultStyleKey();
}
}
private void ApplyNewView()
{
ViewBase view = this.View;
if (view != null)
{
base.DefaultStyleKey = view.DefaultStyleKey;
}
else
{
base.ClearValue(FrameworkElement.DefaultStyleKeyProperty);
}
if (base.IsLoaded)
{
base.ItemContainerGenerator.Refresh();
}
}
}
定义ListView和ListViewItem的样式,自定义视图里的DefaultStyleKey和ItemContainerDefaultStyleKey就是我们这里设置的样式
<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type uc:VirtualStackPanelView}, ResourceId=virtualStackPanelViewDSK}"
TargetType="{x:Type ListView}"
>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/> <Setter Property="ItemContainerStyle"
Value="{Binding (ListView.View).ItemContainerStyle,
RelativeSource={RelativeSource Self}}"/> <Setter Property="ItemTemplate"
Value="{Binding (ListView.View).ItemTemplate,
RelativeSource={RelativeSource Self}}"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<uc:VirtualStackPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
Orientation="{Binding (ListView.View).Orientation, RelativeSource={RelativeSource AncestorType=ListView}}"
SmallChanges="{Binding (ListView.View).SmallChanges, RelativeSource={RelativeSource AncestorType=ListView}}"
ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}"
CacheItemCount="{Binding (ListView.View).CacheItemCount, RelativeSource={RelativeSource AncestorType=ListView}}"
/> </ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="lvItemSelectStyle" TargetType="{x:Type ListViewItem}">
<Style.Resources>
<ResourceDictionary Source="pack://application:,,,/QuickZip.UserControls;component/Themes/Brushes.xaml" />
</Style.Resources>
<!--<Setter Property="Margin" Value="1,2,1,1"/>-->
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Stretch" /> <!--<Setter Property="Background" Value="{TemplateBinding ListViewItem.Background}" />-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="border" BorderBrush="{StaticResource LightBorderBrush}"
BorderThickness="" Padding="" Background="{TemplateBinding Background}" >
<ContentPresenter Margin="5,0" />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource HotTrackBrush}" />
<Setter TargetName="border" Property="Padding" Value="" />
<Setter TargetName="border" Property="BorderThickness" Value="" />
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedBackgroundBrush}" />
<Setter TargetName="border" Property="Padding" Value="" />
<Setter TargetName="border" Property="BorderThickness" Value="" />
</Trigger>
<Trigger Property="uc:SelectionHelper.IsDragging" Value="True">
<Setter Property="Background" Value="{StaticResource HotTrackBrush}" />
<Setter TargetName="border" Property="Padding" Value="" />
<Setter TargetName="border" Property="BorderThickness" Value="" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type uc:VirtualStackPanelView}, ResourceId=virtualStackPanelViewItemDSK}"
TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource lvItemSelectStyle}" >
</Style>
设置我们的视图,在视图中定义我么的ItemTemplate样式,用于填充项容器的ContentPresenter的内容
<uc:VirtualWrapPanelView x:Key="IconView" ColumnHeaderContainerStyle="{StaticResource ColumnHeaderContainerStyle}"
ItemHeight="{Binding RelativeSource={RelativeSource AncestorType=uc:FileList2}, Path=ViewSize, Converter={StaticResource iac}}"
ItemWidth="{Binding RelativeSource={RelativeSource AncestorType=uc:FileList2}, Path=ViewSize, Converter={StaticResource iac}}"
SmallChanges="{Binding Path=ItemHeight, RelativeSource={RelativeSource Self}}"
CacheItemCount=""
HorizontalContentAlignment="Left" >
<uc:VirtualWrapPanelView.ItemTemplate>
<DataTemplate>
<DockPanel Margin="2,1,1,0">
<Image
x:Name="img" DockPanel.Dock="Top" HorizontalAlignment="Center" Stretch= "UniformToFill"
Height="{Binding RelativeSource={RelativeSource AncestorType=uc:FileList2}, Path=ViewSize}"
Width="{Binding RelativeSource={RelativeSource AncestorType=uc:FileList2}, Path=ViewSize}"
Source="{Binding JumboIcon.Item2.Value}"
/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<uc:EditBox x:Name="eb" Margin="0,0"
DisplayValue="{Binding EmbeddedModel.Label}"
ActualValue="{Binding Name, Mode=TwoWay}"
IsEditable="{Binding EmbeddedModel.IsEditable}"
IsEditing="{Binding IsEditing, Mode=TwoWay}"
/>
<TextBlock Text="[*]" Visibility="{Binding EmbeddedModel.IsEncrypted, Converter={StaticResource btv}}" />
</StackPanel>
</DockPanel>
</DataTemplate>
</uc:VirtualWrapPanelView.ItemTemplate>
<uc:VirtualWrapPanelView.Columns>
<GridViewColumn Width="" Header="{x:Static trans:Texts.strHeaderFile}" uc:FileList2.SortPropertyName="sortByFullName" />
<GridViewColumn Width="" Header="{x:Static trans:Texts.strHeaderType}" uc:FileList2.SortPropertyName="sortByType" />
<GridViewColumn Width="" Header="{x:Static trans:Texts.strHeaderTime}" uc:FileList2.SortPropertyName="sortByLastWriteTime" />
<GridViewColumn Width="" Header="{x:Static trans:Texts.strHeaderSize}" uc:FileList2.SortPropertyName="sortByLength" />
</uc:VirtualWrapPanelView.Columns>
</uc:VirtualWrapPanelView>
ListView 视图(View)的更多相关文章
- 视图不能由多个 ListView 共享 (View can't be shared by more than one ListView) 的一个解决方法
1.问题的起因 在WPF中遇到一异常,如题. 因此做一个Demo代码来复现问题,代码如下: <Window x:Class="WpfAppThread.MainWindow" ...
- ListView视图缓存错位问题
由于之前写Scroller应用:ListView滑动删除遇到Item视图错位问题,观察发现第1item位置改变后,第1+10的item布局也跟着改变.假设使用ScrollView+ListView,把 ...
- 使用mvc时,在视图view中使用强类型视图,在web.config文件中添加命名空间namespace的引用不起作用,解决方法
这是view中的model代码: @model t_user_info 这是web.config配置文件只的代码: <namespaces> <add namespace=" ...
- SQL Server 索引(index) 和 视图(view) 的简单介绍和操作
--索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...
- UIViewController的生命周期(根视图view从无到有的过程)
UIViewController的生命周期实质上是指根视图view从无到有的过程 1.首先新建一个工程:不从mainstoryBoard加载 (删除入口) 在AppDelegate.m --> ...
- 视图(View)与部分视图(Partial View)之间数据传递
写ASP.NET MVC程序,我们经常需要把数据从视图(View)传递至部分视图(Partial View) 或者相反. 今天Insus.NET使用 ControllerBase.TempData 进 ...
- MVC中视图View向控制器传值的方法
MVC中视图View向控制器传值的方法步骤如下: 1.index页面: 页面中只需要一个触发事件的按钮
- Oracle 学习笔记 11 -- 视图 (VIEW)
本次必须学习一个全新的概念-- 视图 (VIEW).在前面的笔记中曾提到过,数据对象包含:表.视图.序列.索引和同义词.前面的笔记都是对表的想剖析,那么本次笔记就对视图的世界进行深入的剖析. 视图是通 ...
- Android编程动态创建视图View的方法
在Android开 发中,在Activity中关联视图View是一般使用setContentView方法,该方法一种参数是使用XML资源直接创 建:setContentView (int layout ...
- 关于Android界面编程与视图(View)组件
UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...
随机推荐
- 闪回之 Flashback Query (dml表、过程、函数、包等)、Flashback version Query
Flashback Query 背景:Flashback 是 ORACLE 自 9i 就开始提供的一项特性,在 9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数 ...
- 缓存与数据库一致性之二:高并发下的key重建(先淘汰cache再写db)的问题
一.为什么数据会不一致 回顾一下上一篇文章<缓存与数据库一致性之一:缓存更新设计>中对缓存.数据库进行读写操作的流程. 写流程: (1)先淘汰cache (2)再写db 读流程: (1)先 ...
- Python文件运行时报TabError: inconsistent use of tabs and spaces in indentation
1. 问题描述 Python文件运行时报TabError: inconsistent use of tabs and spaces in indentation 2. 问题原因 tab 和 space ...
- git push -u origin master error: failed to push some refs to
1.问题描述 $ git push -u origin master To github.com:[github_name]/[github_repository_name].git ! [rejec ...
- JavaScript高级程序设计学习笔记第十三章--事件
事件冒泡: IE 的事件流,事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档).例如: <!DOCTYPE html> <htm ...
- 【245】◀▶IEW-Unit10
Unit 10 Censorship 1. Model1题目及范文分析 Some parents believe that there is no harm in allowing their chi ...
- lua调用c函数
参考:http://blog.163.com/madahah@126/blog/static/170499225201121504936823/ 1.编辑C程序 vim luac.c #include ...
- SQL中的drop,truncate和delete的区别
(1) DELETE语句执行删除的过程是每次从表中删除一行,并且同时将该行的删除操作作为事务记录在日志中保存以便进行进行回滚操作.TRUNCATE TABLE 则一次性地从表中删除所有的数据并不把 ...
- SEO优化之——rel=canonical(整合重复页)
用法一 如图所示 有三个页面,三个页面的访问路径都不一样,但是现实的内容page2和page3显示的内容和page1一样,这时候在三个page中分别都加入了<link res="can ...
- 解码H264文件的一些基础知识
这段时间一直在进行编写H264文件的解析类,因此对于H264文件的格式有了初步的了解,官方文档也看了个大概.这篇文章主要是总结了一些为解码H264文件而需要的一些前期知识,话不多说,下面是干货,有些是 ...