自定义视图,设置默认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)的更多相关文章

  1. 视图不能由多个 ListView 共享 (View can't be shared by more than one ListView) 的一个解决方法

    1.问题的起因 在WPF中遇到一异常,如题. 因此做一个Demo代码来复现问题,代码如下: <Window x:Class="WpfAppThread.MainWindow" ...

  2. ListView视图缓存错位问题

    由于之前写Scroller应用:ListView滑动删除遇到Item视图错位问题,观察发现第1item位置改变后,第1+10的item布局也跟着改变.假设使用ScrollView+ListView,把 ...

  3. 使用mvc时,在视图view中使用强类型视图,在web.config文件中添加命名空间namespace的引用不起作用,解决方法

    这是view中的model代码: @model t_user_info 这是web.config配置文件只的代码: <namespaces> <add namespace=" ...

  4. SQL Server 索引(index) 和 视图(view) 的简单介绍和操作

    --索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...

  5. UIViewController的生命周期(根视图view从无到有的过程)

    UIViewController的生命周期实质上是指根视图view从无到有的过程 1.首先新建一个工程:不从mainstoryBoard加载 (删除入口) 在AppDelegate.m --> ...

  6. 视图(View)与部分视图(Partial View)之间数据传递

    写ASP.NET MVC程序,我们经常需要把数据从视图(View)传递至部分视图(Partial View) 或者相反. 今天Insus.NET使用 ControllerBase.TempData 进 ...

  7. MVC中视图View向控制器传值的方法

    MVC中视图View向控制器传值的方法步骤如下: 1.index页面: 页面中只需要一个触发事件的按钮

  8. Oracle 学习笔记 11 -- 视图 (VIEW)

    本次必须学习一个全新的概念-- 视图 (VIEW).在前面的笔记中曾提到过,数据对象包含:表.视图.序列.索引和同义词.前面的笔记都是对表的想剖析,那么本次笔记就对视图的世界进行深入的剖析. 视图是通 ...

  9. Android编程动态创建视图View的方法

    在Android开 发中,在Activity中关联视图View是一般使用setContentView方法,该方法一种参数是使用XML资源直接创 建:setContentView (int layout ...

  10. 关于Android界面编程与视图(View)组件

    UI组件--------------->android.widget.* View组件------------->android.view.* 视图(View)组件 所有UI组件都是建立在 ...

随机推荐

  1. java面试题09

    A卷 1.选择题 public class Test01 { public static void changeStr(String str) { str = "welcome"; ...

  2. div靠右浮动案例

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. CF1076E:Vasya and a Tree

    浅谈树状数组与线段树:https://www.cnblogs.com/AKMer/p/9946944.html 题目传送门:https://codeforces.com/problemset/prob ...

  4. Ubuntu下Apache重启错误:Could not reliably determine解决

    错误信息:apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 ...

  5. wireshark里无网络接口解决办法

    管理员模式下开启npf服务,该服务能捕获网络接口,net start npf

  6. 7.13实习培训日志 Docker

    静态博客github地址 静态博客github地址轻量版 Docker Docker镜像 Docker镜像概念 Docker镜像下载时的分层体现:一层层下载,下载过程中给出了每一层的 ID 的前 12 ...

  7. 使用HTML辅助方法载入分部视图

    在webform中我们用过user control可以减少重复代码也利于将页面模组化, 在mvc中 叫分部视图 Partial View.   也就是一个片段的view.可以利用Partial vie ...

  8. 图解SynchronousQueue原理详解-非公平模式

    SynchronousQueue原理详解-非公平模式 开篇 说明:本文分析采用的是jdk1.8 约定:下面内容中Ref-xxx代表的是引用地址,引用对应的节点 前面已经讲解了公平模式的内容,今天来讲解 ...

  9. [Leetcode]847. Shortest Path Visiting All Nodes(BFS|DP)

    题解 题意 给出一个无向图,求遍历所有点的最小花费 分析 1.BFS,设置dis[status][k]表示遍历的点数状态为status,当前遍历到k的最小花费,一次BFS即可 2.使用DP 代码 // ...

  10. 3dmax tcb控制器

    https://wenku.baidu.com/video/course/v/3a0e059d884c4d0b03bf85441b87311b 7.48开始 tcb控制器比较适合产生平滑动画 张力Te ...