How to make wpf combo box pull a special property
up vote
3
down vote
favorite
I want to use a combo box for entering a plain integer value.
In my application, some numerical values have special meanings, but any numerical value must be able to be entered (typed in).
The combo box must therefore show an edit field with a drop-down arrow to the right.
If the drop-down arrow is clicked, the drop-down must present a list of explanations (strings) for those special numerical values that have special meanings (for example "103 - Wait for next turn"
).
If the user clicks an entry from the drop-down list (instead of typing in the corresponding value directly), the corresponding numerical value must be transferred to the edit field.
I have tried the following:
- I set the ComboBox's
IsEditable
property to true - I bind the
ItemsSource
property to a collection of objects which have both a numerical property (named"Value"
) and a string property (named"Explanation"
). - I set the
DisplayMemberPath
property to"Explanation"
(the name of the string property above)
With that in place, I can freely type in any number I like, and the string explanation of each special number is properly displayed in the drop-down list. This is exactly what I want.
However, when I select one of the explanations from the drop-down list, it's the explanation string that gets transferred to the edit field, not the corresponding numerical value.
Question: How can I make the combo box transfer the numerical property ("Value"
) instead of the explanation text when I click an item from the drop-down list?
c# wpf combobox binding
add a comment |
up vote
3
down vote
favorite
I want to use a combo box for entering a plain integer value.
In my application, some numerical values have special meanings, but any numerical value must be able to be entered (typed in).
The combo box must therefore show an edit field with a drop-down arrow to the right.
If the drop-down arrow is clicked, the drop-down must present a list of explanations (strings) for those special numerical values that have special meanings (for example "103 - Wait for next turn"
).
If the user clicks an entry from the drop-down list (instead of typing in the corresponding value directly), the corresponding numerical value must be transferred to the edit field.
I have tried the following:
- I set the ComboBox's
IsEditable
property to true - I bind the
ItemsSource
property to a collection of objects which have both a numerical property (named"Value"
) and a string property (named"Explanation"
). - I set the
DisplayMemberPath
property to"Explanation"
(the name of the string property above)
With that in place, I can freely type in any number I like, and the string explanation of each special number is properly displayed in the drop-down list. This is exactly what I want.
However, when I select one of the explanations from the drop-down list, it's the explanation string that gets transferred to the edit field, not the corresponding numerical value.
Question: How can I make the combo box transfer the numerical property ("Value"
) instead of the explanation text when I click an item from the drop-down list?
c# wpf combobox binding
Okay, the following is a quite bit hackish: Don't useDisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against theExplanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override theToString
method so it returns Value.
– elgonzo
Nov 15 at 0:01
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I want to use a combo box for entering a plain integer value.
In my application, some numerical values have special meanings, but any numerical value must be able to be entered (typed in).
The combo box must therefore show an edit field with a drop-down arrow to the right.
If the drop-down arrow is clicked, the drop-down must present a list of explanations (strings) for those special numerical values that have special meanings (for example "103 - Wait for next turn"
).
If the user clicks an entry from the drop-down list (instead of typing in the corresponding value directly), the corresponding numerical value must be transferred to the edit field.
I have tried the following:
- I set the ComboBox's
IsEditable
property to true - I bind the
ItemsSource
property to a collection of objects which have both a numerical property (named"Value"
) and a string property (named"Explanation"
). - I set the
DisplayMemberPath
property to"Explanation"
(the name of the string property above)
With that in place, I can freely type in any number I like, and the string explanation of each special number is properly displayed in the drop-down list. This is exactly what I want.
However, when I select one of the explanations from the drop-down list, it's the explanation string that gets transferred to the edit field, not the corresponding numerical value.
Question: How can I make the combo box transfer the numerical property ("Value"
) instead of the explanation text when I click an item from the drop-down list?
c# wpf combobox binding
I want to use a combo box for entering a plain integer value.
In my application, some numerical values have special meanings, but any numerical value must be able to be entered (typed in).
The combo box must therefore show an edit field with a drop-down arrow to the right.
If the drop-down arrow is clicked, the drop-down must present a list of explanations (strings) for those special numerical values that have special meanings (for example "103 - Wait for next turn"
).
If the user clicks an entry from the drop-down list (instead of typing in the corresponding value directly), the corresponding numerical value must be transferred to the edit field.
I have tried the following:
- I set the ComboBox's
IsEditable
property to true - I bind the
ItemsSource
property to a collection of objects which have both a numerical property (named"Value"
) and a string property (named"Explanation"
). - I set the
DisplayMemberPath
property to"Explanation"
(the name of the string property above)
With that in place, I can freely type in any number I like, and the string explanation of each special number is properly displayed in the drop-down list. This is exactly what I want.
However, when I select one of the explanations from the drop-down list, it's the explanation string that gets transferred to the edit field, not the corresponding numerical value.
Question: How can I make the combo box transfer the numerical property ("Value"
) instead of the explanation text when I click an item from the drop-down list?
c# wpf combobox binding
c# wpf combobox binding
edited Nov 14 at 22:31
Olivier Jacot-Descombes
64.7k885136
64.7k885136
asked Nov 14 at 22:13
Martin Christiansen
4901523
4901523
Okay, the following is a quite bit hackish: Don't useDisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against theExplanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override theToString
method so it returns Value.
– elgonzo
Nov 15 at 0:01
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25
add a comment |
Okay, the following is a quite bit hackish: Don't useDisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against theExplanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override theToString
method so it returns Value.
– elgonzo
Nov 15 at 0:01
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25
Okay, the following is a quite bit hackish: Don't use
DisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against the Explanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override the ToString
method so it returns Value.– elgonzo
Nov 15 at 0:01
Okay, the following is a quite bit hackish: Don't use
DisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against the Explanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override the ToString
method so it returns Value.– elgonzo
Nov 15 at 0:01
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Use a data template for showing the explanation text in the drowp-down.
For showing the value in the text edit field, set the attached property TextSearch.TextPath
for the ComboBox to the name of the value property in your "special values" type.
If the ComboBox should not auto-complete possible special values when entering a number, set its IsTextSearchEnabled
property to false. (Note that this would also disable automatic selection of a special value in the drop-down if you happen to enter one in the edit field.)
The definition of the ComboBox should thus look similar to this:
<ComboBox ItemsSource="{Binding ...}"
IsEditable="True"
TextSearch.TextPath="Value"
IsTextSearchEnabled="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Explanation}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Use a data template for showing the explanation text in the drowp-down.
For showing the value in the text edit field, set the attached property TextSearch.TextPath
for the ComboBox to the name of the value property in your "special values" type.
If the ComboBox should not auto-complete possible special values when entering a number, set its IsTextSearchEnabled
property to false. (Note that this would also disable automatic selection of a special value in the drop-down if you happen to enter one in the edit field.)
The definition of the ComboBox should thus look similar to this:
<ComboBox ItemsSource="{Binding ...}"
IsEditable="True"
TextSearch.TextPath="Value"
IsTextSearchEnabled="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Explanation}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
add a comment |
up vote
0
down vote
Use a data template for showing the explanation text in the drowp-down.
For showing the value in the text edit field, set the attached property TextSearch.TextPath
for the ComboBox to the name of the value property in your "special values" type.
If the ComboBox should not auto-complete possible special values when entering a number, set its IsTextSearchEnabled
property to false. (Note that this would also disable automatic selection of a special value in the drop-down if you happen to enter one in the edit field.)
The definition of the ComboBox should thus look similar to this:
<ComboBox ItemsSource="{Binding ...}"
IsEditable="True"
TextSearch.TextPath="Value"
IsTextSearchEnabled="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Explanation}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
add a comment |
up vote
0
down vote
up vote
0
down vote
Use a data template for showing the explanation text in the drowp-down.
For showing the value in the text edit field, set the attached property TextSearch.TextPath
for the ComboBox to the name of the value property in your "special values" type.
If the ComboBox should not auto-complete possible special values when entering a number, set its IsTextSearchEnabled
property to false. (Note that this would also disable automatic selection of a special value in the drop-down if you happen to enter one in the edit field.)
The definition of the ComboBox should thus look similar to this:
<ComboBox ItemsSource="{Binding ...}"
IsEditable="True"
TextSearch.TextPath="Value"
IsTextSearchEnabled="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Explanation}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Use a data template for showing the explanation text in the drowp-down.
For showing the value in the text edit field, set the attached property TextSearch.TextPath
for the ComboBox to the name of the value property in your "special values" type.
If the ComboBox should not auto-complete possible special values when entering a number, set its IsTextSearchEnabled
property to false. (Note that this would also disable automatic selection of a special value in the drop-down if you happen to enter one in the edit field.)
The definition of the ComboBox should thus look similar to this:
<ComboBox ItemsSource="{Binding ...}"
IsEditable="True"
TextSearch.TextPath="Value"
IsTextSearchEnabled="False">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Explanation}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
edited Nov 16 at 14:35
answered Nov 16 at 14:24
elgonzo
4,36911323
4,36911323
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309524%2fhow-to-make-wpf-combo-box-pull-a-special-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Okay, the following is a quite bit hackish: Don't use
DisplayMemberPath
. Since ComboBox is an ItemsControl, to show the explanation piece in the dropdown it is sufficient to create a DataTemplate (with its datatype set to the type of objects in the ItemsSource collection!) with a TextBlock or similar binding against theExplanation
property. Now, when selecting an item in the dropdown (not using DisplayMemberPath), the value placed in the edit field is the string representation of this item. Hence, in the Value/Explanation object type, override theToString
method so it returns Value.– elgonzo
Nov 15 at 0:01
Yes, that was hackish indeed, but hey - it works! I will mark this as an answer unless a less hacky solution shows up. Now only a minor thing is left: When I start to type in a number, the combo box tries for each key stroke to pull in a possible number from the drop-down list. Is there a way to avoid that?
– Martin Christiansen
Nov 15 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 at 14:14
Please see my answer below. I have some good news and some bad news. Bad news first: I apologize for making you override ToString() for no purpose. It is not necessary. Now the good news: It's not necessary to modify your "special value" class and override ToString(). ;-b
– elgonzo
Nov 16 at 14:25