开发者

Bind objects of different types to combobox in WPF

Requirement: 1. Show a list of all referral types in a combo box. (eg. Newspaper, Yellow Pages, Client) 2. Upon choo开发者_JAVA百科sing client, a popup is shown where they can search for clients within the system. 3. From the popup, the user can choose one client. 4. Popup will be dismissed and the client name will be populated into the combobox.

I'm almost done with the implementation, wherein requirements 1 thru 3 are done. However, I'm stuck with number 4. The reason is that, the combobox is bound to referraltype objects. However, the chosen item from the popup is of type "client". Right now, I'm just displaying the chosen client in a separate textblock. However, the customer wants it to be displayed as part of the combobox itself.

Not sure how it could be done.

Any help would be appreciated.

Thanks Bala


You could have a BaseReferralTypeViewModel which NewspaperViewModel, YellowPagesViewModel and ClientViewModel all inherit from. In the combo box resources in xaml declare DataTemplates which describe how to display these types in the combo box. In the case of the first two types its probably just a text block. In the case of the ClientViewModel create an instance but don't initialise it with any client data. The DataTemplate can detect this and just display the referral type. Once the ClientViewModel is initialised with client data by the popup the DataTemplate will detect this and display the name.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜