c# wpf ComboBox 动态下拉框 及 动态默认值设定


1.下拉框声明

<ComboBox x:Name="DirComboBox" Width="150" Height="18" Margin="6,0,0,0">
</ComboBox>

2. 

/// <summary>
/// 初始化下拉框
/// </summary>
public void SetDirComboBox()
{
  DirComboBox.ItemsSource = SessionHelper.Context.DirList;
  DirComboBox.SelectedValuePath = "oid";
  DirComboBox.DisplayMemberPath = "name";
  DirComboBox.SelectedIndex = 0;
  SessionHelper.Context.DirComboBox = DirComboBox;
}

注:Context中

public ObservableCollection<MetaDirVO> DirList = new ObservableCollection<MetaDirVO>();
public ComboBox DirComboBox { get; set; }

 

3.

当DirList新增时,更新ComboBox 的ItemsSource ,并设置默认值:

SessionHelper.Context.DirList.Insert(0,new MetaDirVO(tvs.Id, tvs.Name));
SessionHelper.Context.DirComboBox.ItemsSource = SessionHelper.Context.DirList;
SessionHelper.Context.DirComboBox.SelectedIndex = 0;

 

4. 

用到的VO:

public class MetaDirVO :AbstractModel
{
public MetaDirVO(string oid, string name)
{
this.oid = oid;
this.name = name;
}
public string oid { get; set; }

public string name { get; set ; }

...

}

打赏

免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2020 CODEPRJ.COM