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?










share|improve this question
























  • 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










  • 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















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?










share|improve this question
























  • 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










  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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










  • 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












  • 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












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>





share|improve this answer























    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',
    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
    });


    }
    });














    draft saved

    draft discarded


















    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








    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>





    share|improve this answer



























      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>





      share|improve this answer

























        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>





        share|improve this answer














        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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 at 14:35

























        answered Nov 16 at 14:24









        elgonzo

        4,36911323




        4,36911323






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?