(WPF) MVVM: 动态添加控件及绑定。
比如需要显示一个键盘,里面有各个按键。实现的效果如下:

之前的思路,就是建立一个singleKey的控件,然后在后台用代码动态的添加到父控件里去, 再用代码在后台进行绑定。
这种实现方法并不是真正的MVVM的模式。体会不到MVVM带来的便捷和惊喜。
用MVVM模式来实现时的思路如下:
1. 建立singleKey的ViewModel,定义需要绑定View的属性。
2. 在Key的ViewModel中,使用可观察集合,并绑定到View的ItemsSource上。
ViewModel
public ObservableCollection<DutSingleKeyViewModel> Keysets
{
get
{
return this.keysets;
}
set
{
this.keysets = value;
}
}
3. 对于singleKey的显示,可以在DataTemplat里面定义。
View
<UserControl.DataContext>
<vm:DutKeysetViewModel />
</UserControl.DataContext>
<UserControl.Resources>
<DataTemplate DataType="{x:Type vm:DutSingleKeyViewModel}">
<Canvas>
<Grid Canvas.Left="{Binding KeyLocationLeft, Mode=OneWay}"
Canvas.Top="{Binding KeyLocationTop, Mode=OneWay}"
>
<Rectangle Fill="#FFF4F4F5"
HorizontalAlignment="Left"
Width="{Binding KeySizeWidth}"
Height="{Binding KeySizeHeight}"
Stroke="Black"
VerticalAlignment="Top"></Rectangle>
<TextBlock HorizontalAlignment="Left"
TextWrapping="Wrap"
Text="{Binding KeyCharacter}"
Margin="5,0,0,0"
VerticalAlignment="Top" />
</Grid>
</Canvas>
</DataTemplate>
</UserControl.Resources>
<Grid>
<WrapPanel HorizontalAlignment="Left"
Height="227"
Width="532">
<ItemsControl ItemsSource="{Binding Keysets}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</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.
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 SearchView you create an ItemsControl and bind the ItemsSource to MySearchItems.
<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
<DataTemplate DataType="{x:Type local:SearchEntryViewmodel}">
<StackPanel Orientation="Horizontal">
<Combobox ItemsSource="{Binding MyPropertyInSearchEntryViewmodel}"/>
<!-- the other controls with bindings -->
</StackPanel>
</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里面引用:
<Window.Resources>
<DataTemplate DataType={x:Type viewmodels:MyUserControlViewModel}">
<views:MyUserControlView />
</DataTemplate>
</Window.Resources>
(WPF) MVVM: 动态添加控件及绑定。的更多相关文章
- C# WPF后台动态添加控件(经典)
概述 在Winform中从后台添加控件相对比较容易,但是在WPF中,我们知道界面是通过XAML编写的,如何把后台写好的控件动态添加到前台呢?本节举例介绍这个问题. 这里要用到UniformGrid布局 ...
- WPF 动态添加控件以及样式字典的引用(Style introduction)
原文:WPF 动态添加控件以及样式字典的引用(Style introduction) 我们想要达到的结果是,绑定多个Checkbox然后我们还可以获取它是否被选中,其实很简单,我们只要找到那几个关键的 ...
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
- JQuery动态添加控件并取值
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- winform导入导出excel,后台动态添加控件
思路: 导入: 1,初始化一个OpenFileDialog类 (OpenFileDialog fileDialog = new OpenFileDialog();) 2, 获取用户选择文件的后缀名(s ...
- Android 在布局容器中动态添加控件
这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...
- VC中动态添加控件
VC中动态添加控件 动态控件是指在需要时由Create()创建的控件,这与预先在对话框中放置的控件是不同的. 一.创建动态控件: 为了对照,我们先来看一下静态控件的创建. 放置静态控件时必须先建立一个 ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
- asp.net动态添加控件学习
看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...
随机推荐
- Mvc3.0_笔记
1.保留文本框中的值:<p>Find @Html.TextBox("searchKey", ViewBag.Filter as string)</p> 这 ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
- leetcode 107 Binary Tree Level Order Traversal II ----- java
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java——设计模式(单例模式)
/* * 设计模式: 对问题行之有效的解决方式,其实他是一种思想. * 1.单例设计模式. * 解决问题: 就是可以保证一个列在内存中的对象唯一性. * *如何保证对象的唯一性: *1. 不 ...
- js的数组操作 splice
原文:点击打开链接 1.作用:从指定位置删除部分元素并增加新的元素 1.1.该方法返回值是被删除的元素组成的数组 1.2.splice是直接 ...
- Linux体系结构(二): Linux系统层次
前一节内容大概介绍了一下Linux的历史由来,各大Linux发行厂商都是基于GNU项目下的所有开源软件,来构建各自己的Linux发行版本,一个完整的Linux发行版本大概可以分为以下四个层次结构: ...
- unity, collider/trigger on children
参考:http://answers.unity3d.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.ht ...
- TaffyDB:开源JavaScript数据库
你是否曾经注意到javascript的对象有点像数据库中的记录,你把很多javascript对象包装到一起时就像是你在处理一个数据库中的表,TaffyDB是一个Javascript库,它提供了强大的数 ...
- 【转】Apache Options Indexes FollowSymLinks详解
禁止显示Apache目录列表-Indexes FollowSymLinks如何修改目录的配置以禁止显示 Apache 目录列表.缺省情况下如果你在浏览器输入地址: http://localhost:8 ...
- asp.net 计算两个时间差
两个时间相差多少 .net中的timespan应用2008/11/10 11:54TimeSpan 对象表示时间间隔或持续时间,按正负天数.小时数.分钟数.秒数以及秒的小数部分进行度量.用于度量持续时 ...