8 Replies - 363 Views - Last Post: 07 August 2019 - 09:05 AM Rate Topic: -----

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,184
  • Joined: 12-December 12

string or KeyValuePair

Posted 07 August 2019 - 12:52 AM

I have a central method that attempts to check, for any number of (Silverlight) comboboxes, whether the selected item's value matches the Text of the combobox. (Pointless and redundant exercise I know?!)

Anyway, the selected item is initially an "object" and it might be { 244, Bob Smith } or { ID42, Bob Smith } or { Bob Smith }. Looking for the third example first, I wanted to use if (combo.SelectedItem is string) but this is always false. Why is this false and how can I check if it does, actually, represent a string? *

I believe I can use if (combo.SelectedItem is KeyValuePair<int, string>) and then if (combo.SelectedItem is KeyValuePair<string, string>) but it seems more logical/sensible to me to check for a simple string first.

* I know that it is technically an object, not a string, but in which case why does a test for KeyValuePair work? I would expect both to fail.

Is This A Good Question/Topic? 0
  • +

Replies To: string or KeyValuePair

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7021
  • View blog
  • Posts: 23,845
  • Joined: 05-May 12

Re: string or KeyValuePair

Posted 07 August 2019 - 04:47 AM

How are the comboboxes populated? Is somebody setting the Items property? Are they using the ItemsSource instead?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,184
  • Joined: 12-December 12

Re: string or KeyValuePair

Posted 07 August 2019 - 04:55 AM

Hello. This is an atrocious BI application and the comboboxes are configured in all kinds of ways... although generally by configuring an ItemsSource.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7021
  • View blog
  • Posts: 23,845
  • Joined: 05-May 12

Re: string or KeyValuePair

Posted 07 August 2019 - 05:28 AM

Wow, that making things even worse. If there were one unified way of populating them, then the base object would just need to override their ToString() method and you could just always call combo.SelectItem.ToString().

Anyway, the SilverLight ComboBox documentation says:

Quote

The text box either displays the current selection or is empty if there is no selected item. The following illustration shows a combo box control.

but the fact that you have to do this (pointless) exercise probably means that the documentation is wrong and there are cases when the selected item is not what is being displayed in the textbox.

Anyway in your shoes, I would look at DisplayMemberPath. If it's set, that means what is displayed in the text box is a property of the SelectedItem rather than the actual value. If is not set, then next check the type of the selected item to see whether it is a string or some flavor of KeyValuePair<>.

Edit: Struck out text above. See below.
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7021
  • View blog
  • Posts: 23,845
  • Joined: 05-May 12

Re: string or KeyValuePair

Posted 07 August 2019 - 05:34 AM

Very interesting... Apparently when binding a dictionary to a combobox, DisplayMemberPath needs to be set to "Value"...
Binding to a ComboBox to a Dictionary in Silverlight using MVVM

So that means you'll have to work even harder to figure out what is being displayed...

This is just speculation: If DisplayMemberPath is not not set the the combobox is likely calling ToString() on the items used populate the it.
Was This Post Helpful? 1
  • +
  • -

#6 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,184
  • Joined: 12-December 12

Re: string or KeyValuePair

Posted 07 August 2019 - 06:42 AM

Thank you. DispalyMemberPath is "sometimes" set to Value ;), but there are probably 10 different variations throughout the entire application.

I decided to take the easy route and just test for KeyValuePair (twice) first, then get the string/text value if it isn't a KeyValuePair. As I say, the whole purpose of this central method is likely redundant anyway, but I cannot safely remove or comment it.

I am still curious why it fails to check for a string, but works when checking against a key-value pair. Ah well.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15215
  • View blog
  • Posts: 60,899
  • Joined: 12-June 08

Re: string or KeyValuePair

Posted 07 August 2019 - 07:05 AM

The 'is a string' have to do with 'selecteditem' returns an object up front?

https://docs.microso...%28v%3dvs.95%29
Was This Post Helpful? 0
  • +
  • -

#8 DarenR   User is offline

  • D.I.C Lover

Reputation: 634
  • View blog
  • Posts: 4,201
  • Joined: 12-January 10

Re: string or KeyValuePair

Posted 07 August 2019 - 08:07 AM

i believe combo boxes use .text

cbDivisionDDL.Text to get the text
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,184
  • Joined: 12-December 12

Re: string or KeyValuePair

Posted 07 August 2019 - 09:05 AM

Yes, the method attempts to compare the selected item to .Text.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1