我的情况非常简单,我想知道理想的做法。
我有一个组合框。组合框的每一行对应于特定的策略对象。
将组合框线映射到策略对象的正确方法是什么。
我这样做的方式似乎过于复杂,而且我非常保证有一种简单的标准方法可以做到这一点。
谢谢。
编辑:
我有一个字典中的数据,其中字符串是组合框的文本,对象是策略......但这不是有序的......我只知道有一些非常简单的方法做到这一点。
解: 我使用这个解决方案,在数据类中放置表示逻辑感觉不太舒服:
private partial class HtmlTransformState : AbstractHtmlEditFormState
{
private Dictionary<string, ITransformStrategy> strategies = new Dictionary<string, ITransformStrategy>()
{
{ "Simple URL", new TransformStrategy<SimpleUrlCodeExtractor>() },
{ "Overview", new TransformStrategy<OverviewCodeExtractor>() },
{ "Video List", new TransformStrategy<VideoListCodeExtractor>() },
{ "Video List No MbORKb", new TransformStrategy<VideoListNoMBOrKBAndNoLinksAllowedCodeExtractor>() },
{ "Blue Mountain 2007", new TransformStrategy<BlueMountain2007CodeExtractor>() },
{ "Four Gates", new TransformStrategy<FourGatesCodeExtractor>() },
{ "General", new TransformStrategy<GeneralCodeExtractor>() }
};
public override void DrawForm()
{
// ...
ParentForm.cmboTransformStrategy.DataSource = new BindingSource(strategies, null);
ParentForm.cmboTransformStrategy.DisplayMember = "Key";
ParentForm.cmboTransformStrategy.ValueMember = "Value";
}
public override IEnumerable<string> ProcessHtml(string urlPath)
{
ITransformStrategy transformStrategy = (ITransformStrategy)ParentForm.cmboTransformStrategy.SelectedValue;
// Do some stuff with 'transformStrategy'
}
}
答案 0 :(得分:2)
您的意思是以下内容吗?
public class Strategy
{
private string _name = "default";
public string Name
{
get { return _name; }
set { _name = value; }
}
public Strategy(string name)
{
_name = name;
}
}
然后在表单加载(你需要在该表单上有一个组合框):
private void Form1_Load(object sender, EventArgs e)
{
List<Strategy> ls = new List<Strategy>();
ls.Add(new Strategy("First"));
ls.Add(new Strategy("Second"));
ls.Add(new Strategy("Third"));
comboBox1.DataSource = ls;
comboBox1.DisplayMember = "Name";
}
答案 1 :(得分:0)
我会在组合框上使用SelectedIndexChanged事件并选择相应的字典条目
发现,Bind a Dictionary to a ComboBox见下面的一个工作示例(至少在我写的原始vb.net代码上)
Vb.net转换为C#,您必须自己管理句柄
public class Form1
{
private Dictionary<int, myDic> dict = new Dictionary<int, myDic>();
private void // ERROR: Handles clauses are not supported in C#
ComboBox1_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
KeyValuePair<int, myDic> curItem = (KeyValuePair<int, myDic>)ComboBox1.SelectedItem;
MessageBox.Show(curItem.Value.myvalue);
}
private void // ERROR: Handles clauses are not supported in C#
Form1_Load(object sender, System.EventArgs e)
{
myDic d = default(myDic);
for (int i = 0; i <= 10; i++) {
d = new myDic();
d.myKey = i.ToString;
d.myvalue = Strings.Chr(65 + i);
dict.Add(d.GetHashCode, d);
}
ComboBox1.DataSource = new BindingSource(dict, null);
ComboBox1.DisplayMember = "value";
ComboBox1.ValueMember = "Key";
}
}
class myDic
{
public string myKey;
public string myvalue;
public override string tostring()
{
return myvalue;
}
}
答案 2 :(得分:0)
覆盖策略对象的ToString。之后,您可以直接在组合框中插入策略对象。
public class StrategyObject
{
public override string ToString()
{
return "return the text to display";
}
}
StrategyObject selectedStratObj = comboBox1.SelectedItem as StrategyObject;
答案 3 :(得分:0)
这是我最好的创新之一。 :)我为这个小家伙感到自豪。
public class Stringable<T>
{
private T _obj;
private Func<T, string> _convertFn;
public Stringable(T obj, Func<T, string> convertFn)
{
_obj = obj;
_convertFn = convertFn;
}
public T GetObj() { return _obj; }
public override string ToString() { return _convertFn(_obj); }
}
这个泛型类将ToString()添加到任何类(甚至是黑盒类),您可以在lambda中定义它的行为。想象一下,你有一个具有FirstName和LastName属性的Person。以下是如何使用它来填充组合框。
_cboPersons.Items.Add(new Stringable<Person>(person,o=>string.Format("{0}, {1}", o.LastName, o.FirstName)));
然后,当选择组合框项目时,只需使用它来从组合中获取原始对象
Person person=(_cboPersons.SelectedItem as Stringable<Person>).GetObj() // Get's person object.