Silverlight的空间Combobox的初步使用)

开始很简单的学了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; }
    }
}
最后给出效果吧:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值