How to make wpf combo box pull a special property
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 |
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 '18 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 '18 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 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 '18 at 14:25
add a comment |
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 '18 at 22:31
Olivier Jacot-Descombes
67.6k888140
67.6k888140
asked Nov 14 '18 at 22:13
Martin ChristiansenMartin Christiansen
4921523
4921523
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 '18 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 '18 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 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 '18 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 '18 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 '18 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 at 14:14
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 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 '18 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 '18 at 14:25
add a comment |
1 Answer
1
active
oldest
votes
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 |
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 |
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 '18 at 14:35
answered Nov 16 '18 at 14:24
elgonzoelgonzo
4,54421323
4,54421323
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.
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 '18 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 '18 at 6:32
Add an empty item as fist item of the drop-down list.
– Olivier Jacot-Descombes
Nov 15 '18 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 '18 at 14:25