如何在使用WPF MVVM另一个视图中打开一个视图 [英] How to open one view from another view using MVVM in WPF

查看:200
本文介绍了如何在使用WPF MVVM另一个视图中打开一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来MVVM。而且我不能够得到这个问题的答案的,因为很长一段时间。我不知道,如果问题是如此的困难还是我没有正确解释。我有MainWindow.Xaml其中包含一个文本块和一个按钮,当我按下一个按钮,它应该打开第二个视图称为tableView.xaml(我已经创建用户控制它/ XAML)接收来自文本块中的数据,现在。

I am new to MVVM. And i am not able to get answer of this problem since a long time. I don't know if question is so difficult or i have not explained properly. I have MainWindow.Xaml which contains a textblock and a button to receive the data from textblock and now when i press a button it should open second View called tableView.xaml (I have already created User Control for it/Xaml) .

我现在(请注意,我下面的MVVM在回答)两大问题吗?

(1)如何通过关闭当前打开此视图tableView.xaml从单击按钮打开MainWindow.xaml形式(该按钮点击使用MVVM绑定)?

(1) How to open this View "tableView.xaml" from button click by closing the current opened MainWindow.xaml form (this button click is binded using MVVM) ?

我对按钮的代码在哪里点击这种新形式必须打开(通过关闭当前MainWindow.xaml)在这里(所以我猜码打开tableView.xaml必须某处这里只):

My code for button click where this new form must open (by closing the current MainWindow.xaml) is here (so i guess the code for opening tableView.xaml must be somewhere here only):

 public void SaveExecuted() //some where here i have to open "tableView.Xaml"
 {
   System.Windows.MessageBox.Show(string.Format("Saved: {0} {1} ", DB_Col.DbName, DB_Col.NumTables));
 }
 public ICommand SavePersonCommand
        {
            get
            {
                if (savePersonCommand == null)
                    savePersonCommand = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));
                return savePersonCommand;
            }
        }



但如何做到这一点?

But how to do that ?

(2)tableView.Xaml必须包含动态根据我在以前的MainWindow.xaml接收保存按钮的输入,这将在C#编写的图形用户界面点击和它们必须添加到tableView.Xaml。那么在何处以及如何编写C#代码,以便通过此C#代码生成的图形用户界面将在tableView.Xaml呈现

(2) The "tableView.Xaml" has to contain the GUI which will be written in c# dynamically according to the input i received in previous MainWindow.xaml on "Save Button" click and they must be added to tableView.Xaml. So how and where to write that C# code so that the GUI generated by this c# code will be rendered on tableView.Xaml ?

摘要:可能有人请让我知道如何从按钮点击打开tableView.Xaml以及如何追加/渲染上tableView.Xaml的GUI(我知道如何写C#代码来生成GUI但如果编写代码,这样它。将呈现的GUI tableView.Xaml尊重MVVM规则)

Summary: Could some one please let me know how to open the tableView.Xaml from Button click and how to append/render the GUI on tableView.Xaml (I know how to write the c# code to Generate GUI but where to write that code such that it will render GUI in tableView.Xaml respecting the MVVM rules).

编辑:我觉得还是亲爱的助手无法理解我我试图做的,所以,请看到更多细节:

I feel like still dear helpers are not able to understand what i am trying to do, So please see it for more detail :

什么,我需要做的是:我有MainWindow.xaml其中包含文本框输入表的数量,这是进入该输入后接收来自用户的输入(因为他想创建表的数量)。而用户他就点击保存按钮。现在保存按钮必须关闭MainWindow.xaml,将推出新的用户控件,这是tableView.xaml。假设他进入4和保存。现在,在新推出的tableView.xaml我要秀哪会收到名表文本框中,列NMBR,如果他为列数输入3,那么就应该有3个文本框来接收每一个名字列和数据类型和主键,最后保存按钮。

What i have to do is: I have MainWindow.xaml which contains textbox "Enter number of tables" which is receiving input from user (as Number of tables he want to create).And user after entering this input he will click save button. Now saving the button must close the MainWindow.xaml and will launch new usercontrol which is "tableView.xaml". Suppose he enter 4 and save. Now in newly launched tableView.xaml i have to show text box which will receive "Name Of table", "Nmbr of columns", if he enter 3 for Number of columns, then there should be 3 more text box to receive name of each column and data type and Primary key and save button at last.

和这个GUI现在有重复4次,因为Mainwondow.xaml用户输入4的号码表在起点动态选项。所以我们要重复这个GUI 4次,在tableView.xaml这就是为什么我在C#中的GUI生成编写代码和GUI生成后,我将呈现从C#代码tableView.xaml获得的GUI的每个表项。现在,你明白了吗?如果你知道这样做MVVM使用任何其他方式,那么你是最欢迎给我一个小样本。
通过动态地使用C#代码生成GUI的意思是我必须做的是这样的:

And this GUI now has to repeat 4 times because in Mainwondow.xaml the user enter 4 in "Number of tables" option dynamically at starting. So we have to repeat this GUI 4 times for each table entry in "tableView.xaml" that's why i am writing code in c# for GUI generation and after GUI generation i will render that GUI obtained from C# code to tableView.xaml. Now you understood ? If you know any other way of doing this using MVVM then you are most welcome to give me a little sample. By generating GUI dynamically using C# code mean i have to do something like :

现在下面的图形用户界面的4倍(在一个大容器内循环),并将其渲染tableView.Xaml(我在C#中这样做,因为我要根据用户在MainWindow.xaml形式的选择动态地重复)。

Now when i got Input from user in MainWindow.xaml as 4 the i repeat below GUI 4 times (in for loop inside a big container) and render it to tableView.Xaml (I am doing it in c# because i have to repeat it dynamically according to the user's choice in MainWindow.xaml form).

StackPanel stp = new StackPanel();
TextBlock txt1 = new TextBlock();
stp.Children.Add(txt1);  

这STP必须去tableView.xaml,必须使4 StackPanel中每个包含文本块。

This stp must go to tableView.xaml and must render 4 stackpanel each containing textblock.

推荐答案

1),你还是不是在您的具体要求非常明确,这使得它很难回答这个问题。你说你想关闭当前MainWindow.xaml形式,但你似乎表明你的tableView是一个用户控件。你们是不是要关闭主窗口,并完全用一个完全不同的窗口更换呢?如果是这样那还有什么意义呢?如果原来的主窗口正在关闭你为什么不只是改变了窗口的内容?

1) You're still not being very clear in your exact requirements which makes it very difficult to answer this question. You say you want to "close the current MainWindow.xaml form" yet you seem to indicate your tableView is a UserControl. Are you trying to shut down MainWindow and replace it entirely with a completely different window? If so then what's the point? If the original MainWindow is closing why don't you just change the contents of that Window?

2)这是一个很大的话题,再次你将不得不提供为你想要做什么的更多信息,但一般使用DataTemplating。你开始通过声明视图模型要创建图形用户界面元素,以及包含它们的列表父VM:

2) This is a big topic and again you're going to have to provide more information as to exactly what you're trying to do, but in general you use DataTemplating. You start by declaring view models for the GUI elements you wish to create, as well a parent VM that contains a list of them:

public abstract class GuiItem { } // base class

public class TextBlockVM : GuiItem { }
public class CheckBoxVM : GuiItem { }
public class TextBoxVM : GuiItem { }

public class CustomViewModel : ViewModelBase
{
    private GuiItem[] _GuiItems = new GuiItem[]
    {
        new TextBlockVM{},
        new CheckBoxVM{},
        new TextBoxVM{}
    };
    public GuiItem[] GuiItems { get { return this._GuiItems; } }
}



然后创建一个ItemsControl,绑定的ItemsSource到数组,指定什么面板类型要在绘制它们含有的DataTemplates指定如何在GUI模板每个VM:

Then you create an ItemsControl, binding ItemsSource to the array, specifying what type of panel you want to draw them on and containing DataTemplates that specify how to template each VM in the GUI:

<ItemsControl ItemsSource="{Binding GuiItems}" Margin="10">

    <ItemsControl.Resources>

        <DataTemplate DataType="{x:Type local:TextBlockVM}">
            <TextBlock Text="This is a TextBlock" />
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:CheckBoxVM}">
            <CheckBox>This is a CheckBox</CheckBox>
        </DataTemplate>

        <DataTemplate DataType="{x:Type local:TextBoxVM}">
            <TextBox Width="100" HorizontalAlignment="Left"/>
        </DataTemplate>

    </ItemsControl.Resources>

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

</ItemsControl>



结果:

Result:

显然,这是一个很简单的例子,是想说明的总体思路,在现实世界的应用程序,你还希望字段添加到视图模型指定文本和命令处理程序等。

Obviously this is a very simple example just to show the general idea, in a real-world app you'd also add fields to the view models to specify the text and command handlers etc

这篇关于如何在使用WPF MVVM另一个视图中打开一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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