1、ListBox的单选和多选
默认情况下只支持单选
通过设定其SelectionMode可以支持多选
SelectionMode - 选择模式 [System.windows.Controls.SelectionMode 枚举]
Single - 只允许单选
Multiple - 可以多选(不需要任何辅助键)
Extended - 可以多选(需要 Ctrl 或 Shift 的配合)
2、数据模板和数据绑定
<ListBox x:name="lbPerson" ItemsSource="{Binding}" SelectionMode="Multiple" Height="118" margin="53,55,8,0" VerticalAlignment="top" ScrollVIEwer.HorizontalScrollbarVisibility="HIDden" SelectionChanged="lbPerson_SelectionChanged">
<ListBox.borderBrush>
<linearGradIEntBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradIEntStop color="#FFA3AEB9" Offset="0.058"/>
<GradIEntStop color="#FF617584" Offset="1"/>
</linearGradIEntBrush>
</ListBox.borderBrush>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel OrIEntation="Horizontal">
<TextBlock x:name="content" FontWeight="Bold" Foreground="#0b333c" FontSize="14" textwrapPing="Wrap" Text="{Binding Cname}" WIDth="50" Height="20"/>
<TextBlock x:name="content2" FontWeight="Bold" Foreground="#0b333c" FontSize="14" textwrapPing="Wrap" Text="{Binding CMCODE}" WIDth="100" Height="20"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
/// <summary>
/// 从WebService获取数据,绑定到ListBox的DataContext上
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private voID cbCounty_SelectionChanged(object sender,System.windows.Controls.SelectionChangedEventArgs e)
{
MapClIEnt.ServiceReference1.Region ws = (sender as ComboBox).SelectedItem as MapClIEnt.ServiceReference1.Region;
getMapDataSoapClIEnt clIEnt = new getMapDataSoapClIEnt();
clIEnt.getRYByADDVCDCompleted += new EventHandler<getRYByADDVCDCompletedEventArgs>(clIEnt_getRYByADDVCDCompleted);
clIEnt.getRYByADDVcdasync(ws.RegionCode);
}
voID clIEnt_getRYByADDVCDCompleted(object sender,getRYByADDVCDCompletedEventArgs e)
{
ObservableCollection<FXJGRY> Lists = e.Result;
this.lbPerson.DataContext = Lists;
}
public T FindFirstVisualChild<T>(DependencyObject obj,string childname) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj,i);
if (child != null && child is T && child.GetValue(nameProperty).ToString() == childname)
{
return (T)child;
}
else
{
T childOfChild = FindFirstVisualChild<T>(child,childname);
if (childOfChild != null)
{
return childOfChild;
}
}
}
return null;
}
Dictionary<string,string> dic = new Dictionary<string,string>();
private voID lbPerson_SelectionChanged(object sender,System.windows.Controls.SelectionChangedEventArgs e)
{
ListBoxItem _selectedItem = (ListBoxItem)(lbPerson.ItemContainerGenerator.ContainerFromItem(this.lbPerson.SelectedItem));
TextBlock myTxt = FindFirstVisualChild<TextBlock>(_selectedItem,"content2");
if (!dic.ContainsValue(myTxt.Text.ToString().Trim()))
{
dic.Add(myTxt.Text.Trim(),myTxt.Text.Trim());
}
foreach (keyvaluePair<string,string> kvp in dic)
{
MessageBox.Show(kvp.Value.ToString());
}
}
3、效果如图
总结
以上是内存溢出为你收集整理的Silverlight ListBox 控件使用介绍全部内容,希望文章能够帮你解决Silverlight ListBox 控件使用介绍所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)