为每个列表生成按钮 [英] generate button for each list

查看:59
本文介绍了为每个列表生成按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在静态类(用作全局类)中有一系列列表

I have a series of lists in a static class (used as a global class)

public static class globalClass
{
    public static List<classA> aList = new List<classA>();
    public static List<classB> bList = new List<classB>();
    public static List<classC> cList = new List<classC>();
}

我想为每个列表生成一个xaml按钮,并被告知反射不是一个好主意.这就是我使用反射处理它的方式.

I want to generate a xaml button for each list, and was told reflection was a bad idea. This is how I handled it using reflection.

//get FieldInfo for globalClass
TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(typeof(globalClass));
IEnumerable<FieldInfo> FieldInfoList = typeInfo.DeclaredFields;

foreach (FieldInfo f in FieldInfoList)
{
    //Only look at lists
    if(f.FieldType.ToString().StartsWith("System.Collections.Generic.List`1")){
        StackPanel s = new StackPanel();
        s.Orientation = Orientation.Horizontal;

        TextBlock textBlock = new TextBlock();
        textBlock.FontSize = 45;
        textBlock.Text = f.Name.ToString();

        Button addButton = new Button();
        addButton.Click += delegate(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(addObjectToLibraryPage), f);
        };
        addButton.Margin = new Thickness(10);
        addButton.Name = "addButton";
        addButton.Content = "add";

        Button deleteButton = new Button();
        deleteButton.Click += delegate(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(deleteObjectFromLibraryPage), f);
        };
        deleteButton.Margin = new Thickness(10);
        deleteButton.Name = "deleteButton";
        deleteButton.Content = "delete";

        s.Children.Add(addButton);
        s.Children.Add(deleteButton);

        //add new textBlock and stackpanel to existing xaml
        stackPanel.Items.Add(textBlock);
        stackPanel.Items.Add(s);
    }
}

有没有更清洁的方法来做到这一点?希望我希望能够传递实际的列表,而不是传递FieldInfo.

Is there any cleaner way to do this? Hopefully I would like to be able to pass the actual list instead of a FieldInfo.

我不想单独处理每个列表,因为我最终可能会得到20多个列表,并且都以非常相似的方式使用它们.

I don't want to have to handle each list individually because I may end up with 20+ lists and am using them all in a very similar way.

我想做的事的一个例子:

An example of what I am trying to do:

假设我有一个食品杂货/营养应用程序,并且希望用户能够记录他们在商店中的饮食/需求.他们可以从水果,蔬菜,肉,乳制品,糖果,罐头等产品的列表中选择.

Suppose I have a grocery/nutrition App, and I want users to be able to record what they eat/need from the store. They can select from a list of Fruit, Vegetables, Meat, Dairy, Sweets, Canned Goods, Etc..

但是,我希望他们能够(作为高级选项)能够编辑可能的水果或任何其他食物类别的列表.而且我不想只列出食物",因为肉会记录诸如最低烹饪温度之类的东西.

But, I want them to be able to (as an advanced option) be able to edit the list of possible fruits, or any other food category. And I don't want to just have a list of "food" because meat will record things like minimum cooking temperature or something like that.

因此,在高级选项下,我希望每个类别都有两个按钮(添加到水果,从水果中删除).从理论上讲,添加一个导入/导出"页面,以便我可以与其他人或其他人共享我的水果清单.

So, under advanced options, I would want two buttons for each category (add to fruit, delete from fruit). And theoretically add an Import/Export page so I can share my list of fruits with other people or something.

似乎没有指向使用超类的答案.请参阅: C#多态性简单问题

It doesn't seem like the answers pointing to using a superclass will work. See: C# polymorphism simple question

推荐答案

您可以创建一个列表,以包含您拥有的所有现有列表.然后,您可以遍历列表以创建按钮.如果您希望为每个列表维护一个标签,则可以使用字典,将键作为标签文本,并将列表作为值.

You can create a list to contain all the existing lists you have. You can then iterate over the list to create the buttons. If you wish to maintain a label for each list you could use a dictionary with the key as the label text and list as the value.

除了建议的解决方案外,还应考虑到Sayse的评论.

The proposed solution aside, do take into account the comments given from Sayse.

这篇关于为每个列表生成按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆