设置itemcontrol的item点击前后不同状态
转自:http://www.cnblogs.com/linzheng/p/3764300.html
<Page.Resources>
<!--选中数据项的样式-->
<DataTemplate x:Key="dataTemplateSelectKey" x:Name="dataTemplateSelectName">
<Grid Tapped="StackPanel_Tap_1" Background="Red">
<TextBlock Text="{Binding LastName}" FontSize="50" />
</Grid>
</DataTemplate>
<!--默认数据项的样式,注意默认的数据项样式不能在C#中再次调用-->
<DataTemplate x:Key="dataTemplateDefaultKey" x:Name="dataTemplateDefaultName">
<StackPanel Orientation="Horizontal" Tapped ="StackPanel_Tap_1" x:Name="sp">
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
<!--非选中数据项的样式-->
<DataTemplate x:Key="dataTemplateNoSelectKey" x:Name="dataTemplateNoSelectName">
<StackPanel Orientation="Horizontal" Tapped ="StackPanel_Tap_1">
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Page.Resources>
<StackPanel>
<ItemsControl x:Name="listbox" ItemTemplate="{StaticResource dataTemplateDefaultKey }" ItemsSource="{Binding lstu}"/>
</StackPanel>
private void StackPanel_Tap_1(object sender, TappedRoutedEventArgs e)
{
student stu = (student)(sender as Panel).DataContext;
System.Diagnostics.Debug.WriteLine((sender as Panel).GetType().ToString());
System.Diagnostics.Debug.WriteLine(stu.LastName);
//sender是点击的item,sender as Panel获得该item内的容器对象,分别是StackPanel和Grid
// 然后从listbox里面获取到当前的ContentPresenter对象
ContentPresenter myContentPresenter = (ContentPresenter)(listbox.ContainerFromItem((sender as Panel).DataContext));
// 判断数据模板是选中状态的还是非选中状态的,然后进行赋值
if (myContentPresenter.ContentTemplate.Equals(dataTemplateSelectName))
{
//赋值非选中状态的模板
myContentPresenter.ContentTemplate = dataTemplateNoSelectName;
}
else
{
//赋值选中状态的模板
myContentPresenter.ContentTemplate = dataTemplateSelectName;
}
}
在这里还要注意一点的是,如果使用的时ListBox控件而不是ItemsControl控件的时候,在获取ContentPresenter对象的时候需要通过可视化树去查找。代码的实现如下所示:
private void StackPanel_Tap_1(object sender, TappedRoutedEventArgs e)
{
var s=(sender as Panel).DataContext;
//System.Diagnostics.Debug.WriteLine((sender as Panel).GetType().ToString());
//System.Diagnostics.Debug.WriteLine(stu.LastName);
// 通过点击的控件的DataContext判断所绑定的数据对象
// 然后从listbox里面获取到当前的ContentPresenter对象
ListBoxItem myListBoxItem = (ListBoxItem)(listbox.ContainerFromItem((sender as Panel).DataContext));
// 判断数据模板是选中状态的还是非选中状态的,然后进行赋值
// 在ListBoxItem中查找ContentPresenter
ContentPresenter myContentPresenter=(ContentPresenter)FindVisualChild<ContentPresenter>(myListBoxItem);
if (myContentPresenter.ContentTemplate.Equals(dataTemplateSelectName))
{
//赋值非选中状态的模板
myContentPresenter.ContentTemplate = dataTemplateNoSelectName;
}
else
{
//赋值选中状态的模板
myContentPresenter.ContentTemplate = dataTemplateSelectName;
}
}
//查找可视化树某个类型的元素
private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem childOfChild = FindVisualChild<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
设置itemcontrol的item点击前后不同状态的更多相关文章
- Android ListView的item背景色设置以及item点击无响应等相关问题
Android ListView的item背景色设置以及item点击无响应等相关问题 在Android开发中,listview控件是非常常用的控件,在大多数情况下,大家都会改掉listview的ite ...
- Android Material适配 为控件设置指定背景色和点击波纹效果
Android Material适配 为控件设置指定背景色和点击波纹效果,有需要的朋友可以参考下. 大部分时候,我们都需要为控件设置指定背景色和点击效果 4.x以下可以使用selector,5.0以上 ...
- android 项目学习随笔十三(ListView实现ITEM点击事件,将已读状态持久化到本地)
1.因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为 2,为了使LISTVIEW的ITEM在点 ...
- Android GridView 第一个Item 点击没反应
@Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHo ...
- listview 嵌套checkbox响应item点击和button点击事件
参考文档 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=182280 一.主要要点 1. CheckBox的优先级比item高. ...
- Android设置常见控件点击效果
一. Imageview的点击效果——图片稍微变暗突出点击效果 public class ClickImageView extends AppCompatImageView { public Clic ...
- 设置ListView的item多选
一,首先定义一个类来关联Adapter和Activity能够记住点击的位置 public class MyViewHoler{ public CheckBox cb; public TextView ...
- 从源码角度入手实现RecyclerView的Item点击事件
RecyclerView 作为 ListView 和 GridView 的替代产物,相信在Android界已广为流传. RecyclerView 本是不会有类似 ListView 的那种点击事件,但是 ...
- Android ListView中Item点击事件失效解决方案
欢迎关注公众号,每天推送Android技术文章,二维码如下:(可扫描) 在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布 ...
随机推荐
- 报错:java.lang.IllegalArgumentException: object is not an instance of declaring class
反射的报错信息如下: java.lang.IllegalArgumentException: object is not an instance of declaring class at sun.r ...
- python3精简笔记(二)——函数
函数 下面的地址可以查看函数: https://docs.python.org/3/library/functions.html 也可以在交互式命令行通过help()查看函数的帮助信息. 如: > ...
- FairyGUI编辑器制作Unity3D UI值得借鉴
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- python常用模块之xml模块
python常用模块之xml模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,在json还没诞生的年代,大家都是使用xml,目前很多传统公司的系 ...
- python中读取文件的f.seek()方法
用于二进制文件中F.seek方法 作用: 设置读写位置 F.seek(偏移量, whence=相对位置) 偏移量 大于0的数代表向文件末尾方向移动的字节数 小于0的数代表向文件头方向中移动的字节数 相 ...
- 自己理解的java工厂模式,希望对大家有所帮助
[http://www.360doc.com/content/11/0824/17/3034429_142983837.shtml] 这两天突然想学学java源代码,不过看到一篇文章说看java源代码 ...
- stm32寄存器版学习笔记02 串口通信
stm32F103RCT6提供5路串口.串口的使用,只要开启串口时钟,设置相应的I/O口的模式,然后配置下波特率.数据位长度.奇偶校验等信息,即可使用. 1.串口的配置步骤 ①串口时钟使能 APB2外 ...
- BZOJ1304 CQOI2009 叶子的染色 【树形DP】
BZOJ1304 CQOI2009 叶子的染色 Description 给一棵m个结点的无根树,你可以选择一个度数大于1的结点作为根,然后给一些结点(根.内部结点和叶子均可)着以黑色或白色.你的着色方 ...
- RUAL1519 Formula 1 【插头DP】
RUAL1519 Formula 1 Background Regardless of the fact, that Vologda could not get rights to hold the ...
- linux 下执行python.py 无效解决方案
python 下写linux执行脚本 单独执行 python /home/xx.py 运行正常,但是在linux下 crotch -l 发现有任务运行: 30 0 * * * python /hom ...