读书人

ComboBox里面放置Treeview时选中项的

发布时间: 2012-05-22 18:18:54 作者: rapoo

ComboBox里面放置Treeview时,选中项的问题
在ComboBox里面用多级数据模板绑定TreeView时,选中一项后,ComboBox显示选中项,然后Treeview数据源改变,重新绑定TreeView。结果ComboBox里面的显示的文字没有变,但是选中项是改变了。各位大神,有什么解决方案吗?
页面代码:
<ComboBox Height="22" Name="cbxAccount" HorizontalAlignment="Center" VerticalAlignment="Center" DropDownOpened="cbxAccount_DropDownOpened" DropDownClosed="cbxAccount_DropDownClosed" Width="200">
<ComboBoxItem x:Name="cbItemTreeView" >
<ComboBoxItem.Content>
<controls:TreeView Name="tvAccount" MaxHeight="300" MinHeight="200" MinWidth="150" ItemTemplate="{StaticResource TreeItemTemplate}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectedItemChanged="tvAccount_SelectedItemChanged" BorderBrush="#FFA2ACB8">
</controls:TreeView>
</ComboBoxItem.Content>
</ComboBoxItem>

<ComboBoxItem x:Name="cbItemDisplay" >
</ComboBoxItem>
</ComboBox>
后台绑定的代码:

C# code
void InitTreeView()        {            this.accountTreeInfo.IsExpanded = true;            List<AccountTreeInfo> list = new List<AccountTreeInfo>();            AccountTreeInfo newobj = new AccountTreeInfo();            newobj.AccountName = "请选择代理商";            AccountObj newInfoObj = new AccountObj();            newInfoObj.AccountID = 0;            newobj.AccountObj = newInfoObj;            list.Add(newobj);            list.Add(this.accountTreeInfo);            this.tvAccount.ItemsSource = list;            this.cbItemDisplay.Content = "请选择代理商";            this.cbxAccount.SelectedItem = this.cbItemDisplay;            this.SelectAccount = newobj.AccountObj;        }

结果如下:选中XX公司后,ComboBox显示XX公司,然后调用上面的方法,ComboBox继续显示XX公司。

[解决办法]
将ComboBoxItem清空后台重新加载

读书人网 >CAD教程

热点推荐