开始很简单的学了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();
然后再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>
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的初步使用)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。