自定义视图,设置默认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. 远程调用appium server

    例如:我有两台电脑A(192.168.112.10)和B(192.168.112.11),那我怎么能在A执行本地脚本,但是使用B上的server呢?   查看appium连接appium服务并开启一个 ...

  2. 每天一个Linux命令(2):ls命令

    版权声明 更新:2017-04-26博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下命令ls. 2 开 ...

  3. 1130 host is not allowed to connect to

    mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server 如果你想连接你的mysql的时候发生这个错误: ERROR 1 ...

  4. HDU4348:To the moon

    浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227. ...

  5. 洛谷P1474货币系统——背包方案计数

    题目:https://www.luogu.org/problemnew/show/P1474 完全背包,注意方案计数的方法. 代码如下: #include<iostream> #inclu ...

  6. C# 线程的暂停和恢复的 实现

    我们可以通过ManualResetEvent类来实现. 声明, 初始化时不执行 private static ManualResetEvent _eventWorkList = new ManualR ...

  7. raid0和raid5的 实验过程

    raid:独立的磁盘冗余阵列 创建raid0: 环境准备:准备三块大小相同的磁盘或分区,此处要特别注意:红色字体 [root@localhost6 home]#fdisk /dev/sdd ##对/d ...

  8. 在Spring环境下存取properties文件…

    Spring中PropertyPlaceholderConfigurer的使用 (1) 基本的使用方法是 classpath:/spring/include/dbQuery.properties 其中 ...

  9. 有两种分别用<bgsound>和<embed></embed>标签,当用<embed>插入背景音乐时可以设置宽度和高度为0,隐藏播放器。

     <bgsound>: <bgsound> 是用来插入背景音乐,但只适用于 ie,其参数设定不多.如下 <bgsound src="your.mid" ...

  10. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...