比如需要显示一个键盘,里面有各个按键。实现的效果如下:

之前的思路,就是建立一个singleKey的控件,然后在后台用代码动态的添加到父控件里去, 再用代码在后台进行绑定。

这种实现方法并不是真正的MVVM的模式。体会不到MVVM带来的便捷和惊喜。

用MVVM模式来实现时的思路如下:

1. 建立singleKey的ViewModel,定义需要绑定View的属性。

2. 在Key的ViewModel中,使用可观察集合,并绑定到View的ItemsSource上。

ViewModel

  1. public ObservableCollection<DutSingleKeyViewModel> Keysets
  2. {
  3. get
  4. {
  5. return this.keysets;
  6. }
  7. set
  8. {
  9. this.keysets = value;
  10. }
  11. }

3. 对于singleKey的显示,可以在DataTemplat里面定义。

View

  1. <UserControl.DataContext>
  2. <vm:DutKeysetViewModel />
  3. </UserControl.DataContext>
  4. <UserControl.Resources>
  5. <DataTemplate DataType="{x:Type vm:DutSingleKeyViewModel}">
  6. <Canvas>
  7. <Grid Canvas.Left="{Binding KeyLocationLeft, Mode=OneWay}"
  8. Canvas.Top="{Binding KeyLocationTop, Mode=OneWay}"
  9. >
  10. <Rectangle Fill="#FFF4F4F5"
  11. HorizontalAlignment="Left"
  12. Width="{Binding KeySizeWidth}"
  13. Height="{Binding KeySizeHeight}"
  14. Stroke="Black"
  15. VerticalAlignment="Top"></Rectangle>
  16. <TextBlock HorizontalAlignment="Left"
  17. TextWrapping="Wrap"
  18. Text="{Binding KeyCharacter}"
  19. Margin="5,0,0,0"
  20. VerticalAlignment="Top" />
  21. </Grid>
  22. </Canvas>
  23. </DataTemplate>
  24. </UserControl.Resources>
  25. <Grid>
  26. <WrapPanel HorizontalAlignment="Left"
  27. Height="227"
  28. Width="532">
  29. <ItemsControl ItemsSource="{Binding Keysets}">
  30. <ItemsControl.ItemsPanel>
  31. <ItemsPanelTemplate>
  32. <WrapPanel Orientation="Horizontal" />
  33. </ItemsPanelTemplate>
  34. </ItemsControl.ItemsPanel>
  35. </ItemsControl>
  36. </WrapPanel>

于是,并不用在后台写过多的代码,修改ViewModel后, View的显示就相应变更了。

补充网上大牛的回复。

if you really want to do mvvm , try to forget "how can i add controls". you dont have to, just think about your viewmodels - WPF create the contols for you :)

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

  1. public class SearchEntryViewmodel
  2. {
  3. //Properties for Binding to Combobox and Textbox goes here
  4. }
  5. public class SearchViewModel
  6. {
  7. public ObservableCollection<SearchEntryViewmodel> MySearchItems {get;set;}
  8. public ICommand AddSearchItem {get;}
  9. }

till now you dont have to think about usercontrols/view. in your SearchView you create an ItemsControl and bind the ItemsSource to MySearchItems.

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

you see now all of your SearchEntryViewmodels in the ItemsControl(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

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

thats all :) and you never has to think about how can i add controls dynamically. you just have to add new SearchEntryViewmodel to your collection.

this approach is called Viewmodel First and i think its the easiest way to do MVVM.

在实践发现,通用的方法还是创建一个userControl view,重要的是再DataTemplate里面引用:

  1. <Window.Resources>
  2. <DataTemplate DataType={x:Type viewmodels:MyUserControlViewModel}">
  3. <views:MyUserControlView />
  4. </DataTemplate>
  5. </Window.Resources>

(WPF) MVVM: 动态添加控件及绑定。的更多相关文章

  1. C# WPF后台动态添加控件(经典)

    概述 在Winform中从后台添加控件相对比较容易,但是在WPF中,我们知道界面是通过XAML编写的,如何把后台写好的控件动态添加到前台呢?本节举例介绍这个问题. 这里要用到UniformGrid布局 ...

  2. WPF 动态添加控件以及样式字典的引用(Style introduction)

    原文:WPF 动态添加控件以及样式字典的引用(Style introduction) 我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的 ...

  3. WPF:理解ContentControl——动态添加控件和查找控件

    WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...

  4. JQuery动态添加控件并取值

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. winform导入导出excel,后台动态添加控件

    思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...

  6. Android 在布局容器中动态添加控件

    这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...

  7. VC中动态添加控件

    VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...

  8. jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法

    博客分类: jquery-easyui jQueryAjax框架HTML  现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...

  9. asp.net动态添加控件学习

    看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...

随机推荐

  1. Codeforces Round #136 (Div. 2)

    A. Little Elephant and Function 逆推. B. Little Elephant and Numbers \(O(\sqrt n)\)枚举约数. C. Little Ele ...

  2. Codeforces Round #124 (Div. 2)

    A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...

  3. 【usaco】patrol

    很长时间都没想出来的简单题,看了题解才写出来,还是naive 原题: FJ有个农场,其中有n块土地,由m条边连起来.FJ的养牛场在土地1,在土地n有个新开张的雪糕店.Bessie经常偷偷溜到雪糕店,当 ...

  4. 【NOI2015】软件包管理器

    NOI难得的水题,话说还是T2诶……又学到了线段树的一种新的魔性使用 看sxysxy大神的代码才写出来的,sxysxy_orz 原题: Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包 ...

  5. network Driver , TDI(Transport Driver Interface) Drivers

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff565094(v=vs.85).aspx https://msdn.micros ...

  6. MFC应用程序框架(转)

    对于程序员来说,如果要提高编程效率,一个好用的,功能强大的并且可以得心应手使用的编程工具往往会给我们程序员带来莫大的方便.其实对于现在的编程工具来说,使用哪一种工具都不是问题的关键,重要的是你能够使用 ...

  7. LNMP-查看安装编译时参数

    查看mysql编译参数: cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE 查看apache编译参数: cat $apachehome$/ ...

  8. maxscript,MAXScript Listener下输入print "hi"为什么输出两次

    第一次是print "hi"的输出,第二次是print "hi" 的返回值被输出. 参考:https://davewortley.wordpress.com/2 ...

  9. docker 镜像的保存以及导入

    docker 镜像的保存 docker save -o  davename.tar  images docker 镜像的导入 docker  import -  importname < tar ...

  10. 2016国赛B题小区数据爬取软件

    -------------------------请以任何方式留言给作者,否则视为窃取----------------------------- 看你们找数据找的那么辛苦 我就苦逼的花了1个小时写了个 ...