首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

关于ComboBox的绑定有关问题,取不到selectvalue值

2014-01-19 
关于ComboBox的绑定问题,取不到selectvalue值继承ComboBox做了一个下拉,绑定值时出现加载取不到值的情况弄

关于ComboBox的绑定问题,取不到selectvalue值
继承ComboBox做了一个下拉,绑定值时出现加载取不到值的情况
弄了两个控件测试下

 conectSql.GetDeptCompleted += (comsend, come) =>
                {
                    cbDept.ItemsSource = new Stools().GetTree(come.Result.Where(q => q.CategoryID == 4).ToList(), "Name", "ID", "FParentID", "ID").ToList(); 
                    cbDept.DisplayMemberPath = "Name";
                    cbDept.SelectedValuePath = "ID";
                };

            cbSex.ItemsSource = new UserfulData().GetSex();
            cbSex.DisplayMemberPath = "Name";
            cbSex.SelectedValuePath = "ID";

两个下拉后台绑定值
在另一个获取数据库的方法里为页面绑定数据源
 conectSql.GetEmpCompleted += (comsend, come) =>
                {
                    emp = come.Result.Where(q => q.ID.ToString() == sitem.ID).FirstOrDefault();
                    LayoutRoot.DataContext = emp;
                };

前台代码
 <mycontrol:MyCombobox Grid.Column="1" Grid.Row="2" x:Name="cbDept" SelectedValue="{Binding FUnitID,Mode=TwoWay}" ></mycontrol:MyCombobox>
         <mycontrol:MyCombobox Grid.Column="3" Grid.Row="2" x:Name="cbSex" SelectedValue="{Binding FSex,Mode=TwoWay}" ></mycontrol:MyCombobox>

断点后发现重写的Itemchanged事件有触发,去取不到selectvalue值
 protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            TextBox txtbox = GetTemplateChild("cbtext") as TextBox;
            if (this.SelectedValue != null)
            {
                TreeViewInfo selectinfo = (this.ItemsSource as List<TreeViewInfo>).Where(q => q.ID == this.SelectedValue.ToString()).FirstOrDefault();
                if (selectinfo != null)
                    txtbox.Text = selectinfo.Name;
            }
        }

如果设置断点,一步步操作(拖时间?),就能在触发事件的时候取到值.
开始考虑可能是异步的问题?但发现绑定列表数据时,性别下拉列表的数据是直接绑的,尝试在异步取到数据源后再对combobox进行赋值,还是不能在触发时取到SelectedValue ,这是为什么? combobox
[解决办法]
有点混乱,上传个Demo,帮你测试看看~

热点排行