开发者

Silverlight: How can I set a converter on a ComboBox ItemsSource in the code-behind?

开发者 https://www.devze.com 2023-01-30 22:40 出处:网络
I have a combobox that is populated at runtime with values from a loadoperation (I\'m using RIA services)

I have a combobox that is populated at runtime with values from a loadoperation (I'm using RIA services)

        cboSite.ItemsSource 开发者_开发知识库= lo.Entities;

However, I want to be able to add a null item to the top of the list shown in the combobox, so following the example given here:

http://clr-namespace.com/post/SilverlightWPF-ComboBox-with-Empty-Item-allows-user-to-go-back-to-no-selection.aspx

I am trying to use a converter to insert the item at the top of the list. However, the problem I have is that I can't seem to work out how to specify the converter in the code behind!

Any ideas how to achieve this?


If you are willing to assign ItemsSource from the code-behind you can convert your Entities in the same very place. Something like this:

var converter = new AddEmptyItemConverter();
var converted = (IEnumerable<Entity>)converter.Convert(lo.Entities,
                                                       typeof(IEnumerable<Entity>),
                                                       null,
                                                       null);
 cboSite.ItemsSource = converted;

That Entity should be the type of Entities collection element.

0

精彩评论

暂无评论...
验证码 换一张
取 消