【WPF】ListBox使用DataTemplate 以及默认选中第一项Item
ListBox中DataTemplate的用法如下 。
<ListBox x:Name="areaLB" ItemsSource="{Binding AreaNumList}" SelectedItem="{Binding SelectedItem}" BorderThickness="0" Background="White">
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton x:Name="radioBtn" FontSize="14" GroupName="area" Style="{StaticResource myRadioButton2}">
<RadioButton.IsChecked>
<Binding Path="IsSelected" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" />
</RadioButton.IsChecked>
<RadioButton.Content>
<Binding Path="Content" RelativeSource="{RelativeSource AncestorType=ListBoxItem}" Mode="TwoWay" />
</RadioButton.Content>
</RadioButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
需求:初始化时,使ListBox默认选中第一项。(否则将没有任一项被选中)
过程:由于ListBox使用了DataTemplate,一直是想办法如何去访问/获取到ListBox中的第一项,然后设置该项
RadioButton.isChecked = true;
搜到不少类似下面这种方法的,如何从控件中递归找到子控件,都没法解决问题:
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;
}
最后才发现,直接把ListBox控件的选中项设为第一项即可!其实就是这么简单!!
listBox1.SelectedIndex = 0;
重要的参考:
http://stackoverflow.com/questions/8221795/selecting-first-item-in-listbox
【WPF】ListBox使用DataTemplate 以及默认选中第一项Item的更多相关文章
- Easyui combobox如何默认选中第一项???
以下代码可以实现combobox默认选中第一项,在实际开发中我们可能会用到! // 处理combobox默认选中的问题 <input id="user_type" class ...
- ionic中ng-options与默认选中第一项的问题
1. select中动态添加数据时发现一个选项为空,在选中了其他选项时,在点击时发现第一个空选项消失了,所有我们需要设置一个默认的选项: 2. 开始的时候我用的方法: <select class ...
- Easyui combobox下拉框默认选中第一项
var val = $(#cc).combobox("getData");for (var item in val[0]) { if (item == "gr ...
- jquery easyui combobox设置默认选中第一项
combobox的内容是从后台获取的json, js截取: var data = $('#id').combobox('getData'); $("#id ").combobox( ...
- vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失
vue父路由默认选中第一个子路由,切换子路由让父路由高亮不会消失 正常默认会有 .router-active-class 识别高亮 达到以上注意: 1. exact 不要加 注意是不要加,exact ...
- easyui的combobox默认选中第一个选项
pmProjectSelect.combobox({ editable:false, width:165, url : ctx + '/PmProject/findByProjectMgr', //c ...
- jsp 页面通过jq处理默认 选中的项 数据是通过遍历显示
jsp页面循环显示里面是<a></a>或者<input> id 以什么开头的id,然后当你点击那个的时候就在那个上面添加样式 <div> <di ...
- easyui tree 默认选中第一个元素
系统采用ajax异步调用后端接口做数据呈现,有个场景就是加载的树形菜单默认选中第一行,网上有人分享的解决方案能实现效果(http://aokunsang.iteye.com/blog/1489204) ...
- jquery EasyUi 添加节点、展开所有节点、默认选中第一个节点
感觉easyUi 的树用起来不如 Ext 的树方便,首先,root节点不太好自定义, 异步加载时,只能通过后台判断生成root节点,但是这样一来有一个问题,就是第一次访问界面时, 树的初始化比较慢,大 ...
随机推荐
- 【laravel5.4】查询构造器对象与模型instance的互相换换
1.查询构造器一般情况下返回对象,但是无法直接使用model类的一些方法,如toJson.toArray等 DB::table 结果转换成 model 类实例[collect 实例] public f ...
- 【laravel5.4】Baum无限极分类和collect助手函数、transform()中间件(转换数据)方法使用
1.目的,无限极分类 /* * getdepartment:获取[当前登录用户对应公司的所有有效部门] * DB::table ==>返回查询构造器结果,不会返回一个collect实例 * 而 ...
- C plus plus 控制格式
使用这些格式需要声明包含<iomainip> long flags( ) const 返回当前的格式标志.long flays(long newflag) 设置格式标志为newflag,返 ...
- message 弹出窗口
import javax.swing.JOptionPane;public class gong { public static void main(String [] args){ ...
- Android中的Service使用
Service的生命周期 (适用于2.1及以上) 1. 被startService的无论是否有任何活动绑定到该Service,都在后台运行.onCreate(若需要) -> onStart(in ...
- List Set Map以及子接口用法总结(转)
Collection ├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap └WeakHashMap list 和 ...
- 配置Kafka集群和zookeeper集群
原文链接请参见:http://www.cnblogs.com/5iTech/articles/6043224.html
- if语句的数据驱动优化(Java版)
举个栗子,如果我要输出数字对应的中文描述,我可以用这种方法来写: int num=2; if (num==1){ System.out.println("一"); } else i ...
- win7下安装ubuntu双系统的方法及心得体会(ps:要死好几回的节奏)
1.win7下安装ubuntu系统后,可以很好进入ubuntu系统 但是进不去win7,开机有win7选项,但是选择后不管用 方法思路:各种修复win7的mbr 我的问题是:在winpe中发现,根本看 ...
- 安装ubuntu和windows双系统后,如何修改默认启动项
在安装了Ubuntu16.04系统之后,系统会默认自启动Ubuntu16.04,而我们大多数情况下可能都在使用windows系统,不修改默认设置,不经意间便会启动了Ubuntu16.04,通过我的经历 ...