Pass ComboBox Selected Item as Method Parameter

前端 未结 2 1045
野性不改
野性不改 2021-01-27 10:48

What\'s the proper way to pass a ComboBox Selected Item as a Method Parameter?

Or is there even any advantage to doing this?

I have limited experience in program

相关标签:
2条回答
  • 2021-01-27 11:27

    To get the Selected Item's string you need to call ToString(). Then you can pass the string to any method instead of an object. That being said, you can create a Dictionary<string selected,string opposite> to easily get the opposite color using the selected item's name instead of using if/else or switch statements:

    private static Dictionary<string, string> _oppositesDictionary = new Dictionary<string, string>()
    {
        {"Green", "Red"},
        {"Orange", "Blue"},
        {"Yellow", "Purple"},
        {"Red", "Green"},
        {"Blue", "Orange"},
        {"Purple", "Yellow"}
    };
    

    Usage:

    public string OppositeColor(string color)
    {
        //No need to check if key exists
        return _oppositesDictionary[color];
    }
    
    
    
    private void button_Click(object sender, RoutedEventArgs e)
    {
        string color = cboColors.SelectedItem.ToString();
    
        MessageBox.Show(OppositeColor(color));
    }
    

    Update:

    If you want to perform small tasks based on the color name, use a switch statement.

    If you want to perform different tasks (no repeating code!) based on the color name, you might want to create a Dictionary<string, Action</* params type*/>> or Dictionary<string, Func</* return type*/, /*params type*/>> if you want to return a value.
    Because you have only shown us an example of what you want, I can only think that this is an overkill:

    //Methods have to be static when using a field initializer
    //Func<string> returns a string and has no paramters
    private static Dictionary<string, Func<string>> _colorFunc = new Dictionary<string, Func<string>>()
    {
        {"Green", GreenFunc},
        {"Orange", BlueFunc}
        ....
    };
    
    private static string GreenFunc()
    {
        // green logic
        return "Red";
    }
    
    //Usage
    public string OppositeColor(string color)
    {
        return _colorFunc[color]();
    }
    
    0 讨论(0)
  • 2021-01-27 11:29

    WPF has bindings that can help you with this. If you create a simple helper class to represent your items in the combobox:

    public class ComboItem
    {
      public string Color { get; private set; }
      public string OppositeColor { get; private set; }
    
      public ComboItem(string color, string opposite)
      {
        Color = color;
        OppositeColor = opposite;
      }
    }
    

    And in your code behind have a collection the combobox can bind to:

    private List<ComboItem> _myComboItems = new List<ComboItem>()
    {
      new ComboItem("Red", "Green"),
      new ComboItem("Orange", "Blue"),
      new ComboItem("Yellow", "Purple"),
      new ComboItem("Green", "Red"),
      new ComboItem("Blue", "Orange"),
      new ComboItem("Purple", "Yellow")
    };
    
    public List<ComboItem> MyComboItems
    {
      get { return _myComboItems; }
    }
    

    Implement INotifyPropertyChanged interface to event to the UI when a property in your view changes (without having to implement and dependency properties):

    public event PropertyChangedEventHandler PropertyChanged;
    
    private void OnPropertyChanged(string propertyName)
    {
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    

    And an item to set when the user makes a selection:

    private ComboItem _selected = null;
    
    public ComboItem SelectedComboItem
    { 
      get { return _selected; }
      set
      {
        _selected = value;
        OnPropertyChanged("SelectedComboItem");
      }
    }
    

    you are able to set your xaml to look like:

    <ComboBox ItemsSource="{Binding MyComboItems}"
              SelectedItem="{Binding SelectedComboItem}"
              DisplayMemberPath="Color"/>
    

    Ince this is in place, when the user presses the button (button1), your handler can do what you want it to in a few different ways:

    private void button_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show(SelectedComboItem.OppositeColor);
    }
    

    which accesses the selected items property for opposite color directly, or for your want to pass parameters:

    private void button_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show(GetOppositeColor(SelectedComboItem));
    }
    
    private string GetOppositeColor(ComboItem item)
    {
      if (item != null)
        return item.OppositeColor;
    
      return "No opposite color available";
    }
    

    To set the initial selected item in the combo:

    InitializeComponent();
    // some other initialization code here...
    SelectedComboItem = MyComboItems[0];
    

    This setting of the property SelectedComboItem will cause the PropertyChanged event to fire (through OnPropertyChanged) which the combobo will get and adjust its selected item.

    IMO keeping the types is important for readability and efficiency. Changing an value to type Object then casting back to the specific type it is or using ToString() is less efficient than keeping it as the type it always was to begin with and accessing its value, and it also makes following the code harder when types morph to Object and back again.

    My code example eliminates the use of collections for pairing strings together in favor of using a class to wrap the relationship up. This enables you to utilize WPF's magic to get user interaction events (like selecting an item in a combo) and automatically having access to the object the selection represents (through the SelectedItem binding).

    Hope this helps.

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