
在有一些项目中,UI界面上的控件有时是在程序运行时才生成的。这样的功能在MVVM中也很容易实现。并且可以通过按钮取得其值。
本实例主要实现程序运行时,在界面上生成四个控件,两个TextBox和两个TextBlock.并且点击按钮时,d出TextBox中的值。如下图效果
@H_301_16@
实现方法分以下步骤
第一步:新建一个Sivlerlight应用程序,命名为autoCreatControl
第二步:新建一个viewmodel层,工程名为viewmodel
整个项目结构如下图
通过上面的项目结构图,大家知道需要新建什么文件了
第三步:在工程viewmodel新建一个文件夹viewmodel,并且建一个文件autoControlviewmodel.cs,在此文件中主要viewmodel层的属性和业务。首先要建一个属性,类型为StackPanel,此属性要包含两个TextBox控件和两个TextBlock控件。然后把此属性绑定到主页面即可显示在达到的效果,另外还要有一个绑定到button的属性。详细情况请看代码
using System;using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
namespace viewmodel
{
/// <summary>
/// 自动创建控件的viewmodel
/// </summary>
public class autoControlviewmodel
{
// 定义一个UI变量
StackPanel sp;
public autoControlviewmodel()
{
CreateControl();
}
/// <summary>
/// UI变量属性,用于绑定到VIEw层
/// </summary>
public StackPanel Sp
{
get { return sp; }
set { sp = value; }
}
/// <summary>
/// 创建控件方法,在此方法中根据业务创建控件
/// </summary>
private voID CreateControl()
{
sp = new StackPanel();
sp.OrIEntation = OrIEntation.Vertical;
StackPanel sp1 = new StackPanel();
sp1.OrIEntation = OrIEntation.Horizontal;
TextBlock tb1 = new TextBlock();
tb1.Text = " 用户编号 " ;
tb1.margin = new Thickness( 20 , 20 , 10 , 10 );
TextBox txt1 = new TextBox();
txt1.WIDth = 120 ;
txt1.margin = new Thickness( 20 , 15 , 0 , 10 );
sp1.Children.Add(tb1);
sp1.Children.Add(txt1);
sp.Children.Add(sp1);
StackPanel sp2 = new StackPanel();
sp2.OrIEntation = OrIEntation.Horizontal;
TextBlock tb2 = new TextBlock();
tb2.Text = " 用户姓名 " ;
tb2.margin = new Thickness( 20 , 5 , 10 );
TextBox txt2 = new TextBox();
txt2.WIDth = 120 ;
txt2.margin = new Thickness( 20 , 10 );
sp2.Children.Add(tb2);
sp2.Children.Add(txt2);
sp.Children.Add(sp2);
}
/// <summary>
/// 绑定到button上的Command上
/// </summary>
public ICommand OkbuttonCommand
{
get { return new autoControlCommand( this ); }
}
/// <summary>
/// 执行button事件方法
/// </summary>
/// <param name="obj"></param>
public voID OkbuttonClick( object obj)
{
UIElementCollection uc = obj as UIElementCollection;
foreach (UIElement cc in uc)
{
if (cc != null )
{
if (cc.GetType().name == " ContentControl " )
{
ContentControl ccontrol = cc as ContentControl;
StackPanel spck = ccontrol.Content as StackPanel;
int count = spck.Children.Count;
for ( int i = 0 ; i < count; i ++ )
{
if (sp.Children[i].GetType().name == " StackPanel " )
{
StackPanel spChild = sp.Children[i] as StackPanel;
for ( int k = 0 ; k < spChild.Children.Count; k ++ )
{
if (spChild.Children[k].GetType().name == " TextBox " )
{
TextBox txt = spChild.Children[k] as TextBox;
MessageBox.Show(txt.Text);
}
}
}
}
}
}
}
}
}
}
第四步:在工程viewmodel中新建一个文件夹Command,然后再建一个文件autoControlCommand.cs,在此文件中实现新口ICommand,实现在点击button时,d出一个对话框。代码如下
using System;using System.Net;
using System.windows;
using System.windows.Controls;
using System.windows.documents;
using System.windows.Ink;
using System.windows.input;
using System.windows.Media;
using System.windows.Media.Animation;
using System.windows.Shapes;
namespace viewmodel
{
public class autoControlCommand:ICommand
{
autoControlviewmodel acvm;
public autoControlCommand(autoControlviewmodel acv)
{
acvm = acv;
}
public bool CanExecute( object parameter)
{
return true ;
}
public event EventHandler CanExecuteChanged;
public voID Execute( object parameter)
{
if (parameter != null )
acvm.OkbuttonClick(parameter);
}
}
}
第五步:在MainPage.xaml中实现数据的绑定。其中绑定自动生成控件时,使用了ContentControl类,
ContentControl类表示包含单项内容的控件。像button,CheckBox和ScrollVIEw 这样的控件直接或间接继承自该类,ContentControl 的 Content 属性可以是任何类型的对象,例如字符串、UIElement 或 DateTime。当 Content 设置为 UIElement 时,ContentControl 中将显示 UIElement。当 Content 设置为其他类型的对象时,ContentControl 中将显示该对象的字符串表示形式。
通过对 ContentControl类的介绍,知道ContentControl的强大了吧,那么咱们就把viewmodel中的Sp属性绑定到ContentControl的Content即可实现自动生成控件
MainPage.xaml@H_502_1004@
< UserControl x:Class = " autoCreatControl.MainPage "xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d = " http://schemas.microsoft.com/Expression/blend/2008 "
xmlns:mc = " http://schemas.openxmlformats.org/markup-compatibility/2006 "
xmlns:local = " clr-namespace:viewmodel;assembly=viewmodel "
mc:Ignorable = " d "
d:DesignHeight = " 311 " d:DesignWIDth = " 400 " >
< UserControl.Resources >
< local:autoControlviewmodel x:Key = " autoviewmodel " />
</ UserControl.Resources >
< GrID x:name = " LayoutRoot " Background = " White " DataContext = " {StaticResource autoviewmodel} " >
< GrID.Children >
< ContentControl Content = " {Binding Sp} " />
< button Content = " 确定 " Height = " 25 " WIDth = " 70 " margin = " 143,87,187,198 " Command = " {Binding OkbuttonCommand} " CommandParameter = " {Binding Children,Elementname=LayoutRoot,Mode=OneWay} " />
</ GrID.Children >
</ GrID >
</ UserControl >
@H_502_1004@通过以上代码就可以达到在MVVM模式中实现自动运行时生成控件的效果。
点击下载源程序
总结以上是内存溢出为你收集整理的SilverLight:使用MVVM实现View层在程序运行时自动生成控件并且取得其值全部内容,希望文章能够帮你解决SilverLight:使用MVVM实现View层在程序运行时自动生成控件并且取得其值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)