自定义视图,设置默认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>

最新文章

  1. PHP preg_replace使用例子
  2. (转)【推荐】初级.NET程序员,你必须知道的EF知识和经验
  3. C#的类,构造函数以及Array阵列的数据填充与绑定
  4. Uva1398 Meteor
  5. 【mysql】统计库、表大小
  6. [转]jquery Fancybox丰富的弹出层效果
  7. jquery 解析xml字符串
  8. 【转】ubuntu自动挂载硬盘方法
  9. FolderBrowserDialog(文件夹浏览对话框)
  10. libsvm中的dec_values以及分类结果评分问题
  11. Redis Sentinel 高可用服务搭建
  12. 【PYTHON】用户登录三次错误锁定
  13. 福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
  14. Android 第二波
  15. C语音秋季学习总结
  16. 精读JavaScript模式(六),Memoization模式与函数柯里化的应用
  17. Android 工具视频学习笔记_WDS
  18. Spring MVC 学习笔记11 —— 后端返回json格式数据
  19. 整理OpenResty+Mysql+Tomcat+JFinal+Cannal+HUI
  20. DevExpress v17.2—WPF篇(一)

热门文章

  1. 暑假集训Chapter1 贪心
  2. 【Lintcode】105.Copy List with Random Pointer
  3. Python 静态方法和类方法的区别
  4. 虚拟机ubuntu和windows共享文件
  5. JavaScript总结(1)
  6. exsi thick convert to thin
  7. 删除win7快捷方式小箭头
  8. shell程序---编译目录下全部.c或.cpp文件
  9. LIS与LCS的nlogn解法
  10. CLR via C# 第五章学习记录(更新中)