• 关于silverlight的combobox选择项改变的问题


    昨天接手一个项目,要帮忙做项目里的一个项目统计功能罢了,本来也没什么,麻烦就在于,它要用silverlight.所以然遇到了一路问题。其中之一就是这个[Silverlight中为ComboBox设定当前选择项]。网上搜了大堆资料,其中大同小异,答案如下:
    方法一: this.comboBox1.SelectedItem = (from p in this.comboBox1.Items                                           where (p as Person).Age == 20                                           select p).First(); 方法二: List list = this.comboBoxStatus.ItemsSource as List;             int flag = -1;            for (int i = 0; i < list.Count; i++)             {                 if (list[i].KindId == PDCConst.ATPStatus.NoConsent)                {                    flag = i;                    break;                }             }            this.comboBoxStatus.SelectedIndex = flag; ___________________________________________________

    今天用到了ComboBox控件,像往常一样根据以往使用WebForm的DropDownlist或者WinForm中ComboBox控件的经验来使用Silverlight的ComboBox控件,

    可是遇到麻烦了。

    为ComboBox绑定了某个列表,然后需要根据我当前的值去指定ComboBox的当前选择项。比如说ComboBox绑定了一个List<Employee>.

    Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
    -->1   List<Employee> list =new List<Employee>(){ 2 new Employee(){ EmpID="111", EmpName="1ssssss"}, 3 new Employee(){EmpID="222",EmpName="2dddd"}, 4 new Employee(){EmpID="333",EmpName="3ffff"} 5             }; 6 this.comboBox1.ItemsSource = list; 7 this.comboBox1.DisplayMemberPath ="EmpName";

    现在希望把ID为333的Employee设为当前选择项。不能像以前那样直接Text="3ffff"设定当前值。

    在Siverlight中却有些繁琐。具体的代码:

    Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/
    -->//this.comboBox1.SelectedItem = emp;  //this is not allowed.            Employee emp =new Employee() { EmpID ="333", EmpName ="3ffff" };             List<Employee> list =this.comboBox1.ItemsSource as List<Employee>; int flag =-1; for (int i =0; i

    展示下我页面的那断combobox代码:

    <ComboBox x:Name="cbType" SelectedIndex="0" HorizontalAlignment="Left" Margin="66,158,0,0" VerticalAlignment="Top" Width="120" SelectionChanged="cbType_SelectionChanged" >                     <ListBoxItem Content="电力工程"/>                     <ListBoxItem Content="房地产"/>                     <ListBoxItem Content="水利工程"/>                     <ListBoxItem Content="矿山"/>                     <ListBoxItem Content="交通运输"/>                     <ListBoxItem Content="工业企业"/> </ComboBox>

    然后我就一样样测试的,其中大部分就是调用combobox的SelectionChanged事件进行更改。 无果后 我发现combobox可以添加的items集合有如图六种: 我说死马就当海马医,为何不用其它一种Item类型进行测试 呢,结果把原来的ListItemBox换成了ComboBoxItemk呢。于是乎,之前的这种现象和上面的那个标题的说完全没了。。。

  • 相关阅读:
    解析网页源码方式
    vue踩坑--细节决定成败
    fallowing-travelvue
    学会不怕
    eslint代码规范检测
    三次握手+四次挥手
    小白的学习笔记
    es6数组
    css知识整理
    JavaScript之事件循环,宏任务与微任务
  • 原文地址:https://www.cnblogs.com/meifeng/p/2986851.html
Copyright © 2020-2023  润新知