动态创建文本框MVVM? [英] Dynamically Creating Textboxes MVVM?

查看:139
本文介绍了动态创建文本框MVVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以动态创建文本框在我所取得的C#代码,但人们一直在说我需要遵循MVVM模式,我看着它,它似乎真的很难,我只是不能去适应它。

I can dynamically create Textbox's in C# code which I have achieved, but people have been saying I need to follow the MVVM pattern, I looked into it and it seems really hard and I just can't get used to it.

我需要动态创建文本框,将信息保存在文本框中到SQL,然后才能重新打开它。

I need to dynamically create text boxes, save the information in the text boxes to SQL and then be able to reopen it.

下面是描述什么,我需要做的一个画面:

Here is a picture describing what I need to do:

这是可能的,而不使用MVVM模式呢?

只是需要我怎么能做到这一点推开始和解释升技,我并不想与所有的代码提供。

Just need abit of a push start and explanation of how I can do this, I don't want to be supplied with all the code.

EDIT1:

我不知道这是否是正确的。

I don't know if this is right.

我创建了一个名为'标准',它看起来像这样的类:

I have created a class called 'Standard' which looks like this:

namespace MVVModel
{
public class Standard
{

    string _title;
    string _question;



    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    public string Question
    {
        get { return _question; }
        set { _question = value; }
    }
}
}

现在我要创建一个视图模型?有什么需要在这?

Now I am going to create a ViewModel? What needs to be in this?

推荐答案

您可以使用

<ItemsControl ItemsSource="{Binding StandardCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type Standard}">
            <Grid>
                <TextBox Text={Binding Title} />
                <ItemsControl ItemsSource="{Binding Questions}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBox Text={Binding} />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>



这是绑定到您的视图模型的集合。

Which is binded to any collection in your viewModel.

您可以添加在收集所需的尽可能多的项目

You can add as many items as required in the collection.

更新:

public class Standard
{
    string _title;
    ObservableCollection<string> _questions;        

    public string Title
    {
        get { return _title; }
        set { 
            _title = value;
            NotifyOfPropertyChanged(()=>Title);
        }
    }

    public ObservableCollection<string> Questions
    {
        get { return _questions; }
        set { 
            _questions = value;
            NotifyOfPropertyChanged(()=>Questions);
        }
    }
}

public class StandardViewModel
{
    private ObservableCollection<Standard> _standardCollection;
    public ObservableCollection<Standard> StandardCollection{
        get
        {
            return _standardCollection;            
        }
        set{
            _standardCollection = value;
            NotifyOfPropertyChanged(()=>StandardCollection);
        }
    }
}



看你的图1:它好像你可能有每个标题多个问题。因此,这里是解决方案。

Looking at your Diagram 1: Its seems you may have multiple questions for each title. So here is the solution.

是的,你需要在标准类,使之简单。

Yes, You will need the Standard class to make it simple.

对不起,我没有Visual Studio的,现在,我只写了这个代码在记事本,粘贴在这里。不知道的错误。但是,仅仅是高层次的想法。

Sorry, I don't have Visual Studio right now, I just wrote this code in NotePad, and pasted here. Not sure about errors. But just are high level idea.

这篇关于动态创建文本框MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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