在 WPF MVVM 中动态添加控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11095189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:26:51  来源:igfitidea点击:

Adding controls dynamically in WPF MVVM

wpfmvvm-light

提问by spiritqueen

I am working on a dynamic search view wherein clicking a button should add a new row containing 3 combobox and 2 textboxes.

我正在处理一个动态搜索视图,其中单击一个按钮应该添加一个包含 3 个组合框和 2 个文本框的新行。

How should I go about doing this?

我该怎么做呢?

回答by blindmeis

If you really want to do mvvm , try to forget "how can I add controls". You don't have to, just think about your viewmodels - WPF create the contols for you :)

如果您真的想做 mvvm ,请尝试忘记“如何添加控件”。您不必,只需考虑您的视图模型 - WPF 为您创建控件:)

In your case lets say we have a SearchViewModel and a SearchEntryViewmodel.

在您的情况下,假设我们有一个 SearchViewModel 和一个 SearchEntryViewmodel。

public class SearchEntryViewmodel
{
    //Properties for Binding to Combobox and Textbox goes here
}


public class SearchViewModel 
{
    public ObservableCollection<SearchEntryViewmodel> MySearchItems {get;set;}
    public ICommand AddSearchItem {get;}
}

Till now you dont have to think about usercontrols/view. In your SearchViewyou create an ItemsControland bind the ItemsSourceto MySearchItems.

到目前为止,您不必考虑用户控件/视图。在您SearchView创建一个ItemsControl并将其绑定ItemsSourceMySearchItems.

<ItemsControl ItemsSource="{Binding MySearchItems}"/> 

You see now all of your SearchEntryViewmodelsin the ItemsControl(just the ToString() atm).

您现在可以SearchEntryViewmodelsItemsControl(just the ToString() atm).

To fit your requirements to show every SearchEntryViewmodel with 3Comboboxes and so on you just have to define a DataTemplate in your Resources

为了满足您的要求以使用 3Comboboxes 等显示每个 SearchEntryViewmodel,您只需在资源中定义一个 DataTemplate

<DataTemplate DataType="{x:Type local:SearchEntryViewmodel}">
    <StackPanel Orientation="Horizontal">
        <Combobox ItemsSource="{Binding MyPropertyInSearchEntryViewmodel}"/>
        <!-- the other controls with bindings -->
    </StackPanel>
</DataTemplate>

That's all :) and you never have to think about "how can I add controls dynamically?". You just have to add new SearchEntryViewmodelto your collection.

这就是全部 :) 并且您永远不必考虑“如何动态添加控件?”。您只需SearchEntryViewmodel要向您的收藏中添加新内容。

This approach is called Viewmodel Firstand I think it's the easiest way to do MVVM.

这种方法称为Viewmodel First,我认为这是执行 MVVM 的最简单方法。

回答by Mare Infinitus

If you are new to both MVVM and WPF, there is a really wonderful video tutorial on how to architect a C# / WPF / MVVM application by Jason Dollinger which is available here on lab49. All of the sourcecode he developes in this amazing video is available also right here on lab49.

如果您是 MVVM 和 WPF 的新手,可以在 lab49 上获得Jason Dollinger提供的关于如何构建 C#/WPF/MVVM 应用程序的非常精彩的视频教程。他在这段精彩视频中开发的所有源代码也可以在 lab49 上找到。

After watching it, you will not have any problems developing your search view for sure.

看完之后,您肯定不会在开发搜索视图时遇到任何问题。

回答by R76

One option is that you can create TextBoxes and comboboxes in backend by creating a new instanse. But the better option is that you can create one usercontrol which contains All texboxes and comboboxes which you want to add and in which format you want. After creating when the button is pressed you can create a instace of this usercontrol and set it in the grid or any other control by using SetValueproperty of the control.

一种选择是您可以通过创建新实例在后端创建文本框和组合框。但更好的选择是您可以创建一个用户控件,其中包含您想要添加的所有文本框和组合框以及您想要的格式。在按下按钮时创建后,您可以创建此用户控件的实例,并使用控件的SetValue属性将其设置在网格或任何其他控件中。

If you are new to WPF and MVVM this read this blogs to understand this.

如果您不熟悉 WPF 和 MVVM,请阅读此博客以了解这一点。

https://radhikakhacharia.wordpress.com/2012/06/01/wpf-tutorial-3/

https://radhikakhacharia.wordpress.com/2012/06/01/wpf-tutorial-3/

https://radhikakhacharia.wordpress.com/2012/02/13/model-view-viewmodel/

https://radhikakhacharia.wordpress.com/2012/02/13/model-view-viewmodel/