[WPF]如何正确地用代码设置ListBox的当前选中项
2009-01-12 23:56
501 查看
有人可能会说这有什么好写的。不就是一行代码就能搞定的吗?而且为什么需要用代码设置SelectedItem呢?用户所点的Item不就自动是SelectedItem吗?在这里将要讨论我们的,就是ListBox自己没有能自己把SelectedItem设置正确的情况。本来想当作一个WPF Bug清单的一篇文章的,但是又感觉也许就是有这样变态的需求呢。
我们用一个非常简单的代码的XAML就可以重现这个问题。
<Window x:Class="SelectListBoxItem.DemoWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:s="clr-namespace:System;assembly=mscorlib"
5 Title="ListBox Selection Problem"
6 SizeToContent="Height"
7 Width="300">
8 <ListBox>
9 <ListBox.ItemTemplate>
10 <DataTemplate>
11 <TextBox Text="{Binding .}" Width="100"/>
12 </DataTemplate>
13 </ListBox.ItemTemplate>
14 <s:String>a</s:String>
15 <s:String>b</s:String>
16 <s:String>c</s:String>
17 </ListBox>
18</Window>
运行的效果如下。
using System.Diagnostics;
2using System.Windows;
3using System.Windows.Controls;
4
5namespace SelectListBoxItem
6
其中FindAncestor是自己定义的一个方法,因为单纯地使用VisualTreeHelper是不足以在所有情况下找到Parent的。代码可参见源代码。
写好了怎么用呢?我们说了,要以对现有代码最小的变动实现这个功能。可能有人已经想到了,用Style,那个Window的代码根本不用动。只要在App.xaml里加上一个Resource就OK了。代码如下,简单吧。
<Application x:Class="SelectListBoxItem.App"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:l="clr-namespace:SelectListBoxItem"
5 StartupUri="DemoWindow.xaml">
6 <Application.Resources>
7 <Style TargetType="{x:Type ListBox}">
8 <Setter Property="l:ListBoxService.AutoSelect" Value="True"/>
9 </Style>
10 </Application.Resources>
11</Application>
12
到此,ListBox的行为算是正常些了。正常的运行截图就不发了。
我们用一个非常简单的代码的XAML就可以重现这个问题。
<Window x:Class="SelectListBoxItem.DemoWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:s="clr-namespace:System;assembly=mscorlib"
5 Title="ListBox Selection Problem"
6 SizeToContent="Height"
7 Width="300">
8 <ListBox>
9 <ListBox.ItemTemplate>
10 <DataTemplate>
11 <TextBox Text="{Binding .}" Width="100"/>
12 </DataTemplate>
13 </ListBox.ItemTemplate>
14 <s:String>a</s:String>
15 <s:String>b</s:String>
16 <s:String>c</s:String>
17 </ListBox>
18</Window>
运行的效果如下。
using System.Diagnostics;
2using System.Windows;
3using System.Windows.Controls;
4
5namespace SelectListBoxItem
6
其中FindAncestor是自己定义的一个方法,因为单纯地使用VisualTreeHelper是不足以在所有情况下找到Parent的。代码可参见源代码。
写好了怎么用呢?我们说了,要以对现有代码最小的变动实现这个功能。可能有人已经想到了,用Style,那个Window的代码根本不用动。只要在App.xaml里加上一个Resource就OK了。代码如下,简单吧。
<Application x:Class="SelectListBoxItem.App"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:l="clr-namespace:SelectListBoxItem"
5 StartupUri="DemoWindow.xaml">
6 <Application.Resources>
7 <Style TargetType="{x:Type ListBox}">
8 <Setter Property="l:ListBoxService.AutoSelect" Value="True"/>
9 </Style>
10 </Application.Resources>
11</Application>
12
到此,ListBox的行为算是正常些了。正常的运行截图就不发了。
相关文章推荐
- 如何根据当前手机系统设置的不同语言执行不同的代码?
- wpf中如何改变Listbox选中项的颜色
- [K/3Cloud] 如何代码中动态设置当前活动页签
- [VB.NET]请问如何取得多选中的listBox当前选中的文本内容?
- WPF 如何在代码中设置显示所选 language 的值
- [K/3Cloud] 如何代码中动态设置当前活动页签
- WPF中如何通过代码设置DockPanel.Dock属性
- CListCtrl虚拟列表如何通过代码设置选中某条记录
- 如何通过代码设置WPF控件的字体,颜色
- eclipse如何设置高亮代码的背景色,比如选中某个单词,高亮所有的
- Wpf Datagrid 设置了模板 checkbox 如何一次选中,单击就选中。默认是要双击才得
- c#中DataGridView 如何设置 才能选中一行 设置鼠标事件
- Android Studio如何设置代码自动提示
- 【WPF】ListBox使用DataTemplate 以及默认选中第一项Item
- 自定义RecycleView实现TV应用上的item焦点获取以及设置当前选中的item居中
- 如何设置eclipse格式化代码时不自动换行
- 如何不让IE提示——当前安全设置会使计算机有风险
- 如何设置其它进程中listctrl控件CHECKBOX的选中状态
- K3 如何将物料属性中的科目代码设置默认值
- Xcode如何拖拽选中文字、拖拽代码