将ComboBox选定项目作为方法参数传递的正确方法是什么?
或者这样做有什么好处吗?
我在程序设计和传递参数方面经验有限。
这是一个返回所选颜色的相反颜色的示例方法。
<ComboBox x:Name="cboColors"
HorizontalAlignment="Left"
Margin="179,82,0,0"
VerticalAlignment="Top"
Width="120"
SelectedIndex="0">
<System:String>Red</System:String>
<System:String>Orange</System:String>
<System:String>Yellow</System:String>
<System:String>Green</System:String>
<System:String>Blue</System:String>
<System:String>Purple</System:String>
</ComboBox>
硬编码
ComboBox Selected Item在if语句中设置
// Find Opposite Color
//
public String OppositeColor()
{
if ((string)cboColors.SelectedItem == "Red")
{
return "Green";
}
else if ((string)cboColors.SelectedItem == "Orange")
{
return "Blue";
}
else if ((string)cboColors.SelectedItem == "Yellow")
{
return "Purple";
}
else if ((string)cboColors.SelectedItem == "Green")
{
return "Red";
}
else if ((string)cboColors.SelectedItem == "Blue")
{
return "Orange";
}
else if ((string)cboColors.SelectedItem == "Purple")
{
return "Yellow";
}
else
{
return string.Empty;
}
}
// Display Opposite Color Button
//
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(OppositeColor());
}
传递参数
ComboBox Selected Item设置为Object,然后传递给Method
// Find Opposite Color
//
public String OppositeColor(Object color)
{
if (color.Equals("Red"))
{
return "Green";
}
...
}
// Display Opposite Color Button
//
private void button_Click(object sender, RoutedEventArgs e)
{
// Set Selected Color
Object color = cboColors.SelectedItem;
// Display
MessageBox.Show(OppositeColor(color));
}
答案 0 :(得分:2)
WPF具有可以帮助您解决此问题的绑定。 如果您创建一个简单的帮助程序类来表示组合框中的项目:
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;
}
}
在你的代码后面有一个组合框,组合框可以绑定到:
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; }
}
当视图中的属性发生更改时,将事件的INotifyPropertyChanged接口实现到UI:(无需实现和依赖项属性):
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
用户进行选择时要设置的项目:
private ComboItem _selected = null;
public ComboItem SelectedComboItem
{
get { return _selected; }
set
{
_selected = value;
OnPropertyChanged("SelectedComboItem");
}
}
您可以将xaml设置为:
<ComboBox ItemsSource="{Binding MyComboItems}"
SelectedItem="{Binding SelectedComboItem}"
DisplayMemberPath="Color"/>
Ince已经到位,当用户按下按钮(button1)时,你的处理程序可以通过几种不同的方式完成你想要的操作:
private void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(SelectedComboItem.OppositeColor);
}
直接访问相反颜色的所选项属性,或者您想要传递参数:
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";
}
在组合中设置初始选定项目:
InitializeComponent();
// some other initialization code here...
SelectedComboItem = MyComboItems[0];
属性SelectedComboItem的这个设置将导致PropertyChanged事件触发(通过OnPropertyChanged)组合将获得并调整其所选项目。
IMO保留类型对于可读性和效率非常重要。将值更改为Object类型然后回退到它所使用的特定类型或使用ToString()效率低于将其保持为始终以及始终访问其值的类型,并且它还使得在类型时更难以遵循代码变形为对象并再次返回。
我的代码示例消除了使用集合将字符串配对在一起,以支持使用类来包装关系。这使您可以利用WPF的魔力获取用户交互事件(如选择组合中的项目)并自动访问选择所代表的对象(通过SelectedItem绑定)。
希望这有帮助。
答案 1 :(得分:1)
要获取所选项目的字符串,您需要致电ToString()
。然后,您可以将字符串传递给任何方法而不是对象。话虽如此,您可以创建Dictionary<string selected,string opposite>
以使用所选项目的名称轻松获得相反的颜色,而不是使用if/else
或switch
语句:
private static Dictionary<string, string> _oppositesDictionary = new Dictionary<string, string>()
{
{"Green", "Red"},
{"Orange", "Blue"},
{"Yellow", "Purple"},
{"Red", "Green"},
{"Blue", "Orange"},
{"Purple", "Yellow"}
};
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));
}
如果要根据颜色名称执行小任务,请使用switch语句。
如果您想根据颜色名称执行不同任务(无重复代码!),您可能需要创建Dictionary<string, Action</* params type*/>>
或Dictionary<string, Func</* return type*/, /*params type*/>>
返回一个值
因为你只向我们展示了你想要的一个例子,我只能认为这是一种矫枉过正的行为:
//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]();
}