原文: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 事件

在此元素聚焦于键盘时发生。由于此事件使用隧道路由,因此具有焦点的元素可能是子元素,而不是实际附加事件处理程序的元素。 请检查事件数据中的 Source 以确定实际具有焦点的元素。

http://msdn.microsoft.com/zh-cn/library/system.windows.uielement.previewgotkeyboardfocus.aspx

对于方法2:

UIElement.IsKeyboardFocusWithin 属性

获取一个值,该值指示键盘焦点是否位于元素或其可视树子元素内的任意位置。这是一个依赖项属性。

http://msdn.microsoft.com/zh-cn/library/system.windows.uielement.iskeyboardfocuswithin(v=VS.90).aspx?ppud=4

WPF ListBoxItem模板中添加CheckBox选中问题的更多相关文章

  1. WPF: 在ListView中添加Checkbox列表

    描述:ListView是WPF中动态绑定工具的数据容器,本文实现了一个在ListView中显示的供用户选择的列表项目,并且控制列表中选择的项目数量,即实现单选. XAML中创建ListView,代码如 ...

  2. DataGridView 中添加CheckBox和常用处理方式 .

    DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...

  3. DataGridView中添加CheckBox列用于选择行

    DataGridView中添加CheckBox列用于选择行 1,编辑DataGridView,添加一列 CheckBox ,Name 赋值为 "select",如下图: 2,取消 ...

  4. 往另外1个ListView中添加当前选中的项目

      //往另外1个ListView中添加当前选中的项目   function AddSelItems(listview1:TListView;ListView2:TListView):Boolean; ...

  5. Jquery动态在td中添加checkbox

    如图:想要在这个id为headId的<td>中,用jquery动态添加checkbox 代码如下 : data是我用ajax 从后台获取的数据,里面含有若干个user类,我想把所有的人名字 ...

  6. JQuery获取指定元素中的checkbox选中状态的一些属性

    项目中用户上传病例数据,每一次上传自动生成一个病例文件夹,数据保存到后台,前端显示文件夹,现在的需求是勾选想要删除的文件夹的chenckbox,点击删除后,数据库和前端都相应的更新. 如果是静态页面, ...

  7. 如何在TFS的过程模板中添加报表

    在新建团队项目的过程中,TFS的"新建团队项目向导"会根据用户选择的过程模板类型(CMMI, Scrum,Agile等)自动为团队项目创建一个SSRS(SQL Server Rep ...

  8. js获取到的页面中的checkbox选中的项

    需求描述:列表第一列是checkbox name和value都是id 想通过复选框的勾选状态来获取id,在js中获取 js代码: var checkId=$("input[name='che ...

  9. 在django模板中添加jquery

    路径 /project_name /app_name /templates /index.html /project_name setting.py /static /js jquery.js 导入方 ...

随机推荐

  1. ZOJ FatMouse' Trade 贪心

    得之我幸,不得,我命.仅此而已. 学姐说呀,希望下次看到你的时候依然潇洒如故.(笑~) 我就是这么潇洒~哈哈. 感觉大家比我还紧张~ 我很好的.真的 ------------------------- ...

  2. LOG4J中日志级别的使用

    <logger name="demo-log" additivity="false"> <level value="${log.le ...

  3. [RxJS] Convert RxJS Subjects to Observables

    The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they ca ...

  4. C# datatable增加行(datarow)数据为另一个datatable中某行

    两个表A和B,两表结构相同.现在需要将A表中部分行拷贝到B表中,直接用 DataTableB.rows.add(dataTableA.rows[0]) 这样的方法式会报"row已经属于A表& ...

  5. ios开发日期的NSDate,NSCalendar分类

    #import <Foundation/Foundation.h> @interface NSDate (XMGExtension) /** */ // @property (nonato ...

  6. source insight totalcmd 中文目录

    @echo off for /F "usebackq delims=" %%a in (`echo %1^^^|iconv -f utf-8 -t gb18030`) do ( s ...

  7. 新版Sublime text3注册码被移除的解决办法

    Sublime Text是风靡世界的文本编辑器,支持多种编程语言,启动时间短,打开文件速度快,插件丰富,让很多程序员爱不释手.但是,对于未注册的Sublime Text, 经常在保存的时候会弹出一个烦 ...

  8. [Grid Layout] Use the repeat function to efficiently write grid-template values

    We can use the repeat() function if we have repeating specifications for columns and rows. With the  ...

  9. UItextfield 动态限制输入的字数

    @property (nonatomic, strong) UITextField *txtName; - (void)viewDidLoad { [super viewDidLoad]; //UIC ...

  10. api的管理工具

    一.常用的api管理工具和测试的方法 1.postman 2.Yapi 3.swagger 4.Rap(阿里)