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
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 _myComboItems = new List()
{
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 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:
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.