Why can't I select a null value in a ComboBox?

后端 未结 10 2045
花落未央
花落未央 2020-12-08 06:11

In WPF, it seems to be impossible to select (with the mouse) a \"null\" value from a ComboBox. Edit To clarify, this is .NET 3.5 SP1.

Here\'s some c

相关标签:
10条回答
  • 2020-12-08 07:02

    I know this answer isn't what you asked for (an explanation of why it doesn't work with the mouse), but I think the premise is flawed:

    From my perspective as a programmer and user (not .NET), selecting a null value is a bad thing. "null" is supposed to be the absence of a value, not something you select.

    If you need the ability explicitly not to select something, I would suggest either the work-around you mentioned ("-", "n.a." or "none" as a value), or better

    • wrap the combobox with a checkbox that can be unchecked to disable the combobox. This strikes me as the cleanest design both from a user's perspective and programmatically.
    0 讨论(0)
  • 2020-12-08 07:11

    I had the same kind of problem we did some work around like adding a value property to the collection item like this :

     public class Bar
    
       {
          public string Name { get; set; }
          public Bar Value
          {
             get { return String.IsNullOrEmpty(Name) ?  null :  this; } // you can define here your criteria for being null
          }
       }
    

    Then while adding items instead of null I use the same object :

      comboBox1.ItemsSource=  new ObservableCollection<Bar> 
            {
                new Bar(),
                new Bar { Name = "Hello" }, 
                new Bar { Name = "World" } 
            };
    

    And instead of selecteditem I bind it to selectedvalue :

    <ComboBox Height="23" Margin="25,40,133,0" DisplayMemberPath="Name"
                  SelectedValuePath="Value" 
                  SelectedValue="{Binding Bar}"
                  Name="comboBox1" VerticalAlignment="Top" />
    

    I know It is not a complete solution, just one workaround I use

    0 讨论(0)
  • 2020-12-08 07:13

    I got a new solution for this question. "USING Mahapps"

      xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
    
    
      <ComboBox x:Name="bars"  **controls:TextBoxHelper.ClearTextButton="True"**
                  DisplayMemberPath="Name" 
                  Height="21" 
                  SelectedItem="{Binding Bar}"/>
    

    You can use the close button to clear the content.

    Thanks.

    0 讨论(0)
  • 2020-12-08 07:13

    Just a guess, but I think it sounds reasonable.

    Assume combobox is using "ListCollectionView" (lcv as its instance) as its item collection, which it should be. If you are a programmer, what you gonna do?

    I will respons to both Keyboard and Mouse.

    Once I get Keyboard input, I use

    lcv.MoveCurrentToNext();
    

    or

    lcv.MoveCurrentToPrevious();
    

    So, sure keyboard works well.

    Then I am working on respons Mouse inputs. And it comes the problem.

    1. I want to listen 'MouseClick' event of my item. But probably, my Item doesn't generated, it is just a placeholder. So when user click on this placeholder, I get nothing.

    2. If I get the event successfully, what's next. I will invoke

      lcv.MoveCurrentTo(selectedItem);

    the "selectedItem" which would be null is not an acceptable parameter here I think.

    Anyway, it's just guessing. I don't have time to debug into it though I am able to. I have a bunch of defects to fix. Good Luck. :)

    0 讨论(0)
提交回复
热议问题