简体   繁体   English

如何在Silverlight的ComboBox中模拟“ AppendDataBoundItems”?

[英]How to emulate “AppendDataBoundItems” in Silverlight's ComboBox?

In Asp.Net Combobox, there is a useful property, AppendDataBoundItems , that causes whatever items are bound to the control to be actually appended to whatever "statically" added ad design time. 在Asp.Net组合框中,有一个有用的属性AppendDataBoundItems ,该属性使绑定到控件的所有项目实际附加到“静态”添加的广告设计时间中。 This is useful for "default" values that the user can specify when no item in the available ones meet the criteria, or to specify a special "null value" item. 当可用项目中的任何项目都不符合条件时,用户可以指定“默认”值,或者用于指定特殊的“空值”项目时,这很有用。

Unfortunately there is no such property in Silverlight ComboBox and there is no way either to be notified when the control has been databound. 不幸的是,Silverlight ComboBox中没有此类属性,并且在控件已进行数据绑定时也无法通知。

I came accross the same problem just in the last few days for Comboboxes that were not mandatory. 在最近几天,我遇到了非强制性的Combobox遇到的相同问题。 The way I dealt with it was to add a null value to the collection of say "Salutations" eg 'Mr','Miss' and so forth. 我处理它的方法是在说“ Salutations”的集合中添加一个空值,例如“ Mr”,“ Miss”等。

Ok, my solution is ugly but it works.. Ideally I would like to have a bindableobject of type T that wraps the ObservableCollection. 好的,我的解决方案很丑陋,但是它可以工作。理想情况下,我希望有一个类型T的可绑定对象,该对象包装了ObservableCollection。 But as always, we have massive time pressures here and this will do for the time being. 但是,与往常一样,我们在这里承受着巨大的时间压力,目前暂时如此。

Added a SalutationDTO to the ObservableCollection as follows. 如下将SalutationDTO添加到ObservableCollection中。

 public static void EnableNullableSalutationChoice(this ObservableCollection<SalutationDTO> salutations)
{
  salutations.Insert(0, NullSalutationChoice);
}

    public static SalutationDTO NullSalutationChoice
{
  get
  {
    return new SalutationDTO {Salutation = " ", SalutationID = null};
  }
}

You may be able to do something by overriding the ComboBox template. 您可以通过覆盖ComboBox模板来执行某些操作。 However, the Silverlight combobox has many issues as it is an immature component. 但是,Silverlight组合框是一个不成熟的组件,因此存在许多问题。 You may be better off implementing your own combobox (or using one of the implementations found on the Internet) and writing this behaviour yourself. 您可能最好实现自己的组合框(或使用Internet上的一种实现)并自己编写此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM