所以,你想要的是有代表的名单,然后一个转换器,将委托转换为方法名。
在你的ViewModel中,让Actions属性返回一个代表列表。使用预定义的Func,这是一种不带参数并返回int的方法:
public IEnumerable<Func<int>> Actions
{
get
{
List<Func<int>> list = new List<Func<int>>();
list.Add(AddFunction);
list.Add(SubstractFunction);
return list;
}
}
接下来,实现一个转换器。通常情况下,转换器是“视图”的一部分,所以把它放在cs文件后面的代码中。这种转换转换Func<int>
为字符串,它使用反射来做到这一点:
[ValueConversion(typeof(Func<int>), typeof(string))]
public class FnConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Func<int> fn = value as Func<int>;
return fn.Method.Name;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
最后,你需要使用转换器在XAML。但为此,您需要指定应用转换器的组合框的项目模板。
<!-- earlier in code define the converter as a resource -->
<Window.Resources>
<src:FnConverter x:Key="conv" />
</Window.Resources>
...
<!-- now the combo box -->
<ComboBox Margin="4" ItemsSource="{Binding Path=Actions}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=., Converter={StaticResource conv}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
说了这样的话,我认为更优雅的解决方案是在视图模型中保留一个MethodInfo列表。使用自定义属性生成此列表。在一些代码下面。请注意以下几点:
- PresentingAttribute是一个自定义属性。它来自System.Reflection.Attribute。它什么都没有。您可以添加参数,如“标签”,“描述”等。
- 装饰你想在组合框中使用`[Presenting]'的方法
- 现在,动作使用反射。请注意筛选谓词的“Where”和lambda,它只返回具有我们自定义属性的方法。
- 您必须修改转换器才能使用MethodInfo。
namespace SO
{
class PresentingAttribute : Attribute
{
}
class FnVM
{
public int numA { get; set; }
public int numB { get; set; }
public IEnumerable<MethodInfo> Actions
{
get
{
return typeof(FnVM).GetMethods().Where(minfo =>
minfo.GetCustomAttribute(typeof(PresentingAttribute)) != null
);
}
}
[Presenting]
public int AddFunction()
{
return numA + numB;
}
[Presenting]
public int MulFunction()
{
return numA * numB;
}
}
}
来源
2013-02-25 21:35:49
Uri
你要在增加功能的'ComboBox'或函数的名称?即。 'string's。 – Guillaume 2013-02-25 07:08:25
函数名称实际上。在这种情况下,组合框将包含AddFunction,SubtractFunction。 – 2013-02-25 07:25:54
简而言之,它应该像我从组合框中选择功能,然后单击确定。点击确定,我不检查什么是combox框的值。我只是做了selectedAction.Exceute(或类似的东西)。我不想如果其他检查,如果combox框字符串是添加然后执行这个否则做到这一点。 – 2013-02-25 07:27:46