C#动态GUI对象生成 [英] C# Dynamic GUI Object Generation

查看:88
本文介绍了C#动态GUI对象生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构建一个 WFA WPF应用程序,我有一个文件正在读取名称列表以及与该名称相关联的值.我希望能够:

1)向用户显示列表
2)让他们单击适当的名称,这使他们可以编辑列表项

我对GUI并不那么熟悉,并且很难将精力集中在事件上.想知道如何动态创建与该文本框中显示的关联对象信息相关的事件.另外,为了格式化,我不确定将其全部放置在我的窗口中如何工作.我可以使用数据库,但是我觉得那太过分了(列表会很小).正在考虑使用哈希图存储对象.

-澄清-

我已经从excel文件中读取了该列表,并创建了一个对象列表(我写的我自己的类).我想做的是填充与每个对象相关联的名称的列表(我认为只有文本框才能完成此工作).我的问题的实质是处理用户选择它们的方式(认为我打开了一个新表单,使他们可以编辑该类持有的值,直接修改该对象,因为该对象将是相同的,因此我不必返回它).我不习惯自己处理事件,更不用说像我计划做的那样动态创建图形元素了.

Building a WFA WPF app and I have a file I am reading a list of names and a value associated with the name. I want to be able to:

1) display the list to a user
2) have them click on the appropriate name, this allows them to edit the list item

I am not that familiar with GUI''s though and am having a hard time wrapping my head around event''s. Was wondering how I would dynamically create events that tied to the associated objects information displayed in that text box. Also then for formatting not sure how that would exactly work for my window I want to place them all in. I could use a database but I feel that would be overkill (the list would be rather small). Was thinking to use a hashmap to store the objects.

-Clarification-

I have read the list from an excel file and created a list of objects(my own class I have written). What I want to do is populate a list of the names associated with each object (I think just text boxes should do the job). The real meat of my question is handling the user selecting them (Thinking I open a new form that allows them to edit the values the class holds, modifying the object directly I should not have to return it since the object will be the same). I am not used to handling events themselves, much less for dynamically created graphical elements like I am planning to do.

推荐答案

在澄清之后,它或多或少简单.

这是要使用的技术:

您可以更好地使用ListBox并用对象填充它.现在,注意:不是对象的名称,而是类本身的对象的实例.此控件允许将任何类型的对象添加为列表项.这样,每个列表项将携带用户单击该项,选择是否还有其他内容时要使用的完整数据集.现在,唯一的问题是:哪些文本将显示为列表项的文本?答案很简单:任何ToString返回.因此,您应该修改所有要在此列表中使用的类:正确覆盖System.Object.ToString().让我们看看:

After clarification, it''s more or less simple.

Here is the technique to use:

You can better use the ListBox and populate it with your objects. Now, attention: not with the names of your objects, but with the instances of the objects of your classes themselves. This control allows objects of any types to be added as list items. In this way, each list item will carry complete set of data to be used when the user clicks on the item, selects if or whatever else. Now, the only problem is: what text will be shown as the text of list item? The answer is simple: whatever ToString returns. So, you should modify all your classes to be used in this list: properly override System.Object.ToString(). Let''s see:

class MyListItemData {
    internal MyListItemData(string name, string description /* ,... */) {
        this.fName = name;
        this.fDescription = description;
        //...
    } 
    internal string Name { get { return fName; } }
    internal string Description { get { return fDescription; } }
    public override string ToString() { return fName; } //this will be shown in UI 
    //...
    string fName, fDescription;
    //...
} 



好的.现在让我们看看如何在事件处理程序中使用它.例如,让我们实现简单的功能:显示所选内容中每个项目的描述.



Good. Now let''s see how it can be used in event handlers. For example, let''s implement simple functionality: showing description of each item in selection.

myListBox.SelectedIndexChanged += (sender, eventArgs) => {
       ListBox listBox = (ListBox)sender; //must be myListBox
       int selected = listBox.SelectedIndex;
       if (selected < 0)
           ShowSelectedItemDescription(null);
       else {
           MyListItemData data = (MyListItemData)(listBox.Items[selected]); //one more type cast, the last one
           ShowSelectedItemDescription(data);
       }      
}

//...

void ShowSelectedItemDescription(MyListItemData data) {
    string value;
    if (data == null)
        value = string.Empty;
    else
        value = data.Description;
    myLabel.Text = value;
    //... ?
}



您可以通过这种方式处理任何其他列表框事件.重要的是:提取与项目关联的数据.一个提示:您可以做更多的事情:您可以根据商品内容执行一些操作.为此,您可以在类中包括一些委托实例,这些委托实例用于添加为列表项.

—SA



You can process any other list box events in this manner. An important thing is: you extract the data associated with the item. One hint: you can do more: you can do some action based on the item content. For this purpose, you can include some delegate instances in your class used to add as the list item.

—SA


这篇关于C#动态GUI对象生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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