开始很简单的学了xmal代码如下:但是报错,不知道为什么?
<ComboBox HorizontalAlignment="left" VerticalAlignment="Bottom" x:name="abc" SelectionChanged="abc_SelectionChanged"> <ComboBoxItem Content="1" IsSelected="True" /> <ComboBoxItem Content="2" /> <ComboBoxItem Content="3" /> </ComboBox>在代码中获取值是报错的:
ComboBoxItem item = abc.SelectedItem as ComboBoxItem; txt.Text = item.Content.ToString();
修改代码:不需要 IsSelected="True"也不需要 ItemsSource="{Binding}"才可以运行,但是我又想启动选择第二项。。。怎么办
然后再cs代码中通过abc.Selectedindex = 2;才有用,哎......
下面看看整个代码吧,很乱,一个是xmal初始内容数据,一个是绑定列表对象:
xmal如下:
<GrID x:name="LayoutRoot" Background="White" Height="228" WIDth="378"> <ComboBox x:name="cbbTest" Height="30" WIDth="100" ItemsSource="{Binding}" displayMemberPath="ID" SelectionChanged="select"/> <TextBlock Height="30" WIDth="100" HorizontalAlignment="Center" VerticalAlignment="Bottom" x:name="txt"></TextBlock> <ComboBox HorizontalAlignment="left" VerticalAlignment="Bottom" x:name="abc" SelectionChanged="abc_SelectionChanged"> <ComboBoxItem Content="1" /> <ComboBoxItem Content="2" /> <ComboBoxItem Content="3" /> </ComboBox> </GrID>
cs.如下:
using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;namespace CombomBox{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); getData(); abc.Selectedindex = 2; } List<Class1> lstSource = new List<Class1>(); private voID getData() { lstSource.Add(new Class1 { ID="1",name="oracle"}); lstSource.Add(new Class1 { ID = "2",name = "java" }); lstSource.Add(new Class1 { ID = "3",name = "linux" }); lstSource.Add(new Class1 { ID = "4",name = "windows 8" }); cbbTest.ItemsSource = lstSource; cbbTest.Selectedindex = 0; } private voID select(object sender,SelectionChangedEventArgs e) { string selectedID = (cbbTest.SelectedItem as Class1).name; txt.Text = selectedID; } private voID abc_SelectionChanged(object sender,SelectionChangedEventArgs e) { ComboBoxItem item = abc.SelectedItem as ComboBoxItem; txt.Text = item.Content.ToString(); } }}
数据类如下:
namespace CombomBox{ public class Class1 { public string name { get ;set;} public string ID { get; set; } }}最后给出效果吧: 总结
以上是内存溢出为你收集整理的Silverlight的空间Combobox的初步使用)全部内容,希望文章能够帮你解决Silverlight的空间Combobox的初步使用)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)