最终实现的效果如下:

添加MenuDataSource.cs,字段ImageStyle是用来标识套用的样式

public class MenuGroup
{
public string GroupTitle { get; set; }
public ObservableCollection<MenuItem> ItemContent { get; set; }
} public class MenuItem
{
public string ImageUrl { get; set; }
public string SubTitle { get; set; }
public string ImageStyle { get; set; }
} public class MenuDataSource
{
private ObservableCollection<MenuGroup> _Sourcedata;
public ObservableCollection<MenuGroup> Sourcedata
{
get { return _Sourcedata; }
set { _Sourcedata = value; }
} public MenuDataSource()
{
Sourcedata = GetDataGroup();
} public ObservableCollection<MenuGroup> GetDataGroup()
{
return new ObservableCollection<MenuGroup>()
{
new MenuGroup
{
GroupTitle = "您的订阅",
ItemContent = new ObservableCollection<MenuItem>
{
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"},
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"},
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"},
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"},
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"},
new MenuItem {ImageUrl = "Image/hot-read.png", SubTitle = "工程投资趋势分析",ImageStyle = "style1"}
}
},
new MenuGroup
{
GroupTitle = "工程投资",
ItemContent = new ObservableCollection<MenuItem>
{
new MenuItem {ImageUrl = "Image/工程投资.png", SubTitle = "工程投资",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/任务维度总表.png", SubTitle = "任务维度总表",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/趋势分析.png", SubTitle = "趋势分析",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/使用情况统计.png", SubTitle = "使用情况统计",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/工程规模.png", SubTitle = "工程规模",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/合作单位维度总表.png", SubTitle = "合作单位维度总表",ImageStyle = "style2"},
}
},
new MenuGroup
{
GroupTitle = "工程进度",
ItemContent = new ObservableCollection<MenuItem>
{
new MenuItem {ImageUrl = "Image/项目维度总表.png", SubTitle = "项目维度总表",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/工程质量.png", SubTitle = "工程质量",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/工程物资.png", SubTitle = "工程物资",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/工程份额.png", SubTitle = "工程份额",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/工程施工进度.png", SubTitle = "工程施工进度",ImageStyle = "style2"},
new MenuItem {ImageUrl = "Image/统计分析.png", SubTitle = "统计分析",ImageStyle = "style2"},
}
}
};
}
}

添加TemplateSelector.cs,用作模块选择器

    /// <summary>
/// 模板选择器
/// </summary>
public class TemplateSelector : DataTemplateSelector
{
/// <summary>
/// 第一种文本显示模版
/// </summary>
public DataTemplate ImageTemplate1 { get; set; }
/// <summary>
/// 第二种图片为主显示模版
/// </summary>
public DataTemplate ImageTemplate2 { get; set; }
/// <summary>
/// 核心方法:根据不同的数据源类型返回给前台不同的样式模版
/// </summary>
/// <param name="item"></param>
/// <param name="container"></param>
/// <returns></returns>
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
MenuItem model = item as MenuItem;
string typeName = model.ImageStyle;
if (typeName == "style1")//根据数据源设置的数据显示模式返回前台样式模版
{
return ImageTemplate1;
}
if (typeName == "style2")
{
return ImageTemplate2;
}
return null;
}
}

添加页面:menu.xaml,在GridView中自定两种样式

        <Grid.Resources>
<CollectionViewSource x:Name="itemcollectSource" Source="{Binding Sourcedata}"
IsSourceGrouped="true" ItemsPath="ItemContent" />
<!--样式1-->
<DataTemplate x:Key="imageStyle1">
<Grid Width="" Height="" Background="#33CCCCCC">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" >
<Image Source="{Binding ImageUrl}" Stretch="UniformToFill"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Foreground="White" Text="{Binding SubTitle}"
FontSize="" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
<!--样式2-->
<DataTemplate x:Key="imageStyle2">
<Grid Width="" Height="">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" >
<Image Source="{Binding ImageUrl}" Stretch="UniformToFill"/>
</Border>
<StackPanel VerticalAlignment="Bottom">
<TextBlock Foreground="White" Text="{Binding SubTitle}"
FontSize="" TextAlignment="Center" Margin="0,0,40,0"/>
</StackPanel>
</Grid>
</DataTemplate>
</Grid.Resources>

GirdViw中根据ImageStyle值套用不同的样式

        <GridView Name="gv_Item" SelectionMode="Single"   Grid.RowSpan=""
ItemsSource="{Binding Source={StaticResource itemcollectSource}}"
SelectedItem="{Binding ItemContent, Mode=TwoWay}"
IsItemClickEnabled="True" Margin="120,140,20,20" ItemClick="ItemView_ItemClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel> <GridView.ItemTemplateSelector>
<dataModel:TemplateSelector ImageTemplate1="{StaticResource imageStyle1}"
ImageTemplate2="{StaticResource imageStyle2}">
</dataModel:TemplateSelector>
</GridView.ItemTemplateSelector>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<TextBlock
AutomationProperties.Name="组名称"
Text="{Binding GroupTitle}" FontSize="" Foreground="White"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,50,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>

最后在menu.xaml.cs绑定数据源

public menu()
{
this.InitializeComponent(); this.DataContext = new MenuDataSource(); }

Metro之GridView控件的使用-绑定不同的模板样式显示的更多相关文章

  1. GridView 控件中如何绑定 CheckBoxList

    需求:设计这样一个页面,在页面上可以选择和展示各省份对应的文明城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可 ...

  2. 在aspx页动态加载ascx页面内容,给GridView控件绑定数据

    在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...

  3. 扩展GridView控件——为内容项添加拖放及分组功能

    引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...

  4. .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...

  5. GRIDVIEW 控件

    http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...

  6. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  7. 027. asp.net中数据绑定控件之 GridView控件

    GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...

  8. GridView控件 Reapter控件 DataList控件 的区别和用法

    ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...

  9. GridView控件RowDataBound事件中获取列字段值的几种途径

    前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...

随机推荐

  1. Symantec Liveupdate Administrator的搭建

      1. My Symantec Products 选择需要更新的产品 2. Source Servers 选择ftp, 下载比较稳定 3. Distribute Center 类似于WSUS中的ap ...

  2. linux下IPTABLES配置

    如果你的IPTABLES基础知识还不了解,建议先去看看. 开始配置 我们来配置一个filter表的防火墙. (1)查看本机关于IPTABLES的设置情况 [root@tp ~]# iptables - ...

  3. thrift

    环境准备: 1.下载window版本的thrift编译器 2.下载idea的thirft插件 3.设置thrift编译工具为:步骤1下载的编译器 4.编写thrift文件 namespace java ...

  4. 【翻译】Kinect v2程序设计(C++) Depth编

    Kinect SDK v2预览版,取得Depth数据的方法说明. 上一节,介绍了通过使用Kinect for Windows SDK v2预览版(以下简称为,Kinect SDK v2预览版)从Kin ...

  5. 类型强转(type cast)

    类型转换有 c 风格的,当然还有 c++风格的.c 风格的转换的格式很简单(TYPEEXPRESSION),但是 c 风格的类型转换有不少的缺点,有的时候用 c 风格的转换是不合适的, 因为它可以在任 ...

  6. 20145317彭垚《Java程序设计》第3周学习总结

    20145317彭垚<Java程序设计>第3周学习总结 教材学习内容总结 第四章 4.1类与对象 4.1.1定义类: new clothes():新建一个对象. class clothes ...

  7. LR中获取当前系统时间方法

    方法一:使用loadrunner的参数化获取当前时间使用lr的参数化,非常方便,对lr熟悉的各位朋友也能马上上手,时间格式也有很多,可以自由选择.步骤:1.将复制给aa的值参数化2.选中abc,使用右 ...

  8. Sphinx+MySQL5.1x+SphinxSE+mmseg

    一.不停止mysql的情况下安装SphinxSE 1.确定mysql版本,下载对应源码包 此处下载5.1.69的mysql源码包 #wget ftp://ftp.ntu.edu.tw/pub/MySQ ...

  9. Delphi XE5 如何与其他版本共存

    如果你想使用Delphi诸如XE4.XE3.XE2.XE之类的版本跟Delphi XE5共存的话,在cglm.ini中简单修改两行就行啦. 找到Delphi XE5的安装根目录C:\Program F ...

  10. Mysql 常用命令集

    1.mysqlbinlog工具使用方法如下: 先使用 show binary logs 查看 在使用导出命令 mysqlbinlog -R -uroot -pxxxx -hxxx.xxx.xxx.xx ...