Metro之GridView控件的使用-绑定不同的模板样式显示
最终实现的效果如下:

添加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控件的使用-绑定不同的模板样式显示的更多相关文章
- GridView 控件中如何绑定 CheckBoxList
需求:设计这样一个页面,在页面上可以选择和展示各省份对应的文明城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可 ...
- 在aspx页动态加载ascx页面内容,给GridView控件绑定数据
在aspx页动态加载ascx页面内容 //加载ascx页面内容Control c1 = this.Page.LoadControl("WebUserControl1.ascx"); ...
- 扩展GridView控件——为内容项添加拖放及分组功能
引言 相信大家对GridView都不陌生,是非常有用的控件,用于平铺有序的显示多个内容项.打开任何WinRT应用或者是微软合作商的网站,都会在APP中发现GridView的使用.“Tiles”提供了一 ...
- .Net语言 APP开发平台——Smobiler学习日志:用Gridview控件设计较复杂的表单
最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的”Smobil ...
- GRIDVIEW 控件
http://www.cnblogs.com/shanymen/archive/2009/05/22/1486654.html GridView控件是.net里的一个显示数据控件,该控件制作很人性化, ...
- asp.net GridView控件的列属性
BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...
- 027. asp.net中数据绑定控件之 GridView控件
GridView控件支持下面的功能: 绑定至数据源控件, 如SqlDataSource 内置排序功能 内置更新和删除功能 内置分页功能 内置行选择功能 可以编程方式访问GridView对象模型以动态设 ...
- GridView控件 Reapter控件 DataList控件 的区别和用法
ASP.NET三大控件: 1.GridView控件:表格视图控件,可以用来绑定结果集或者视图,用起来比较方便和灵活,三个控件中使用最多的控件 用法--- this.gridview1.DataSour ...
- GridView控件RowDataBound事件中获取列字段值的几种途径
前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...
随机推荐
- thinkphp模型没继承model报的错
Call to undefined method RoleModel::query() 错误位置 FILE: H:\www\tpsmarty\shop\Lib\Model\RoleModel.clas ...
- Mui - 全局css
头部(mh) <header class="mui-bar mui-bar-nav"> <a class="mui-action-back mui-ic ...
- Embedded Database service fails to start after installing or migrating to Symantec Endpoint Protection (SEP) 12.1.5 (RU5)
https://support.symantec.com/en_US/article.TECH225587.html
- Javascript 笔记与总结(2-3)Javascript 运算符、控制结构与对象操作
[连接运算符 + ] <script> console.log(1+2+'a'+3+4); </script> 输出: 3a34 [逻辑运算符]返回的是最早能判断表达式结果的那 ...
- linux进程查找及杀死
根据进程名称查找 ps aux|grep python(进程名) 杀死进程: kill -s 9 进程id
- MemcacheQ安装及使用
一.MemcacheQ安装记录1.安装libevent查看是否已经安装了libeventrpm -qa|grep libevent如果没有安装使用yum安装yum install libevent l ...
- Linguistic corpora 种子语料库-待分析对象-分析与更新语料库
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
- P2882 Face The Right Way - USACO07MAR
这道题没有一个比较详细的题解,我来提供一份. 首先我们可以知道,反转区间的顺序对结果没有影响,而且一个区间如果翻转两次以上是没有意义的,所以,问题就变成了求哪些区间需要反转. 我们枚举k.对于每一个k ...
- onmouseenter与onmouseover
简单的说: mouseenter第一次进入这个元素的某个子元素时触发.一旦触发后,在mouseleave之前,鼠标在这个元素的子元素上触发mouseenter事件,不会触发这个元素的mouseente ...
- IOS 开发文件操作——NSFileManager
转自:http://blog.csdn.net/xyz_lmn/article/details/8968213,留着方便查阅 iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像androi ...