WPF ListBoxItem模板中添加CheckBox选中问题
原文:WPF ListBoxItem模板中添加CheckBox选中问题
是这样的,需要一个ListBox来展示照片,并添加一个选中的CheckBox.这就需要对ListBox的ItemTemplate的DataTemplate进行定制.添加一个Image和一个CheckBox.
大概是这样子的.
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Grid Width="250">
<Grid.RowDefinitions>
<RowDefinition Height="200"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="{Binding Photo}" Width="220"/>
<CheckBox Grid.Row="1" Content="命中" IsChecked="{Binding IsTarget}"></CheckBox>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
问题来了,当我选中CheckBox的时候,我希望ListBoxItem跳转到当前CheckBox所在的ListBoxItem上.如何实现?
主要有下面两种方法 :
1.
在Xaml中添加:
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>
</ListBox.ItemContainerStyle>
在.cs文件中添加
protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
{
ListBoxItem item = (ListBoxItem)sender;
item.IsSelected = true;
}
2.
在Xaml中添加:
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="true">
<Setter Property="IsSelected" Value="true" />
</Trigger>
</Style.Triggers>
</Style>
问题解决.
解释:
对于方法1:
UIElement.PreviewGotKeyboardFocus 事件
UIElement.IsKeyboardFocusWithin 属性
http://msdn.microsoft.com/zh-cn/library/system.windows.uielement.iskeyboardfocuswithin(v=VS.90).aspx?ppud=4
WPF ListBoxItem模板中添加CheckBox选中问题的更多相关文章
- WPF: 在ListView中添加Checkbox列表
描述:ListView是WPF中动态绑定工具的数据容器,本文实现了一个在ListView中显示的供用户选择的列表项目,并且控制列表中选择的项目数量,即实现单选. XAML中创建ListView,代码如 ...
- DataGridView 中添加CheckBox和常用处理方式 .
DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...
- DataGridView中添加CheckBox列用于选择行
DataGridView中添加CheckBox列用于选择行 1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",如下图: 2,取消 ...
- 往另外1个ListView中添加当前选中的项目
//往另外1个ListView中添加当前选中的项目 function AddSelItems(listview1:TListView;ListView2:TListView):Boolean; ...
- Jquery动态在td中添加checkbox
如图:想要在这个id为headId的<td>中,用jquery动态添加checkbox 代码如下 : data是我用ajax 从后台获取的数据,里面含有若干个user类,我想把所有的人名字 ...
- JQuery获取指定元素中的checkbox选中状态的一些属性
项目中用户上传病例数据,每一次上传自动生成一个病例文件夹,数据保存到后台,前端显示文件夹,现在的需求是勾选想要删除的文件夹的chenckbox,点击删除后,数据库和前端都相应的更新. 如果是静态页面, ...
- 如何在TFS的过程模板中添加报表
在新建团队项目的过程中,TFS的"新建团队项目向导"会根据用户选择的过程模板类型(CMMI, Scrum,Agile等)自动为团队项目创建一个SSRS(SQL Server Rep ...
- js获取到的页面中的checkbox选中的项
需求描述:列表第一列是checkbox name和value都是id 想通过复选框的勾选状态来获取id,在js中获取 js代码: var checkId=$("input[name='che ...
- 在django模板中添加jquery
路径 /project_name /app_name /templates /index.html /project_name setting.py /static /js jquery.js 导入方 ...
随机推荐
- u8和unsigned char的区别
- mysql数据库 navicat premium mac 破解教程
https://www.jianshu.com/p/f3ef78deadaa 转自Navicat Premium for Mac v12.0.22.0 破解教程,macOS上手动破解,无需补丁,无毒 ...
- 洛谷 P3112 后卫马克Guard Mark
->题目链接 题解: 贪心+模拟 #include<algorithm> #include<iostream> #include<cstring> #incl ...
- 并发队列ConcurrentLinkedQueue 和 阻塞队列LinkedBlockingQueue用法
在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出).Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQ ...
- arm cpu的架构及分类说明
今天在编译mplayer for mx27ads的时候, 碰到了armv5te与armv6优化的问题. 默认的交叉编译器支持armv5te也支持armv6,就默认使用了mplayer中mpeg4的ar ...
- PatentTips - Integrated circuit well bias circuitry
1. Field of the Invention This invention relates in general to an integrated circuit and more specif ...
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- html中如何实现表格移入移出时背景颜色改变?(两种方法)
html中如何实现表格移入移出时背景颜色改变?(两种方法) 一.总结 1.通过css的table标签的hover属性: 10 #tab:hover{ 11 background: green 12 } ...
- 使用搜狐Sendcloud的Webapi发送邮件:Jodd和Apache Httpclient
最近,在使用搜狐Sendcloud发邮件. Sendcloud提供http格式的webapi,方便地发送邮件,当然是要付费的. 很早之前,http工具一直用Httpclient,后来觉得jodd ...
- Freemarker中的null判断,小坑一枚
上次写到,在Freemarker中日期转换,可以提取成工具方法.主要是Freemarker对null的处理方式,非常坑爹.只要一个对象或对象的属性为null,就报错,虽然不影响界面显示,但控制台和日志 ...