zoukankan      html  css  js  c++  java
  • 笔记03 wpf 在MVVM模式下怎样在Viewmodel里面获得view的控件对象

     转自http://blog.csdn.net/qing2005/article/details/6601199
    http://blog.csdn.net/qing2005/article/details/6601475

    MVVM中轻松实现Command绑定(二)传递Command参数


    属性栏里去设置的。语句应该是CommandParameter="{Binding ElementName=控件名}"

    我们如果需要在Command中传递参数,实现也很简单。DelegateCommand还有一个DelegateCommand<T>版本,可以传递一个T类型的参数。

    1.View的Button绑定,其中CommandParameter定义了一个“20”的参数

    [html] view plaincopy
     
    1. <Window x:Class="WpfApplication1.Window1"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.         xmlns:vm="clr-namespace:WpfApplication1"  
    5.         Title="Window1" Height="193" Width="190">  
    6.     <Window.DataContext>  
    7.         <vm:Window1ViewModel />  
    8.     </Window.DataContext>  
    9.     <Grid>  
    10.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,20,0,0" Name="button1" VerticalAlignment="Top" Width="109"  
    11.                 Command="{Binding ButtonCommand}"  
    12.                 CommandParameter="20"/>  
    13.     </Grid>  
    14. </Window>  


    2.ViewModel定义命令,注意委托参数

    [csharp] view plaincopy
     
    1. namespace WpfApplication1 {  
    2.     public class Window1ViewModel {  
    3.   
    4.         public ICommand ButtonCommand {  
    5.             get {  
    6.                 return new DelegateCommand<string>((str) => {  
    7.                     MessageBox.Show("Button's parameter:"+str);  
    8.                 });  
    9.             }  
    10.         }  
    11.   
    12.     }  
    13. }  


    并且,特殊情况下,我们可以将控件对象作为参数传递给ViewModel,注意{Binding RelativeSource={x:Static RelativeSource.Self}}是绑定自己(Button)的意思。

    [html] view plaincopy
     
    1. <Window x:Class="WpfApplication1.Window1"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.         xmlns:vm="clr-namespace:WpfApplication1"  
    5.         Title="Window1" Height="193" Width="190">  
    6.     <Window.DataContext>  
    7.         <vm:Window1ViewModel />  
    8.     </Window.DataContext>  
    9.     <Grid>  
    10.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,20,0,0" Name="button1" VerticalAlignment="Top" Width="109"  
    11.                 Command="{Binding ButtonCommand}"  
    12.                 CommandParameter="20"/>  
    13.         <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="34,85,0,0" Name="button2" VerticalAlignment="Top" Width="109"  
    14.                 Command="{Binding ButtonCommand2}"  
    15.                 CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}"/>  
    16.     </Grid>  
    17. </Window>  

    再看ViewModel

    [csharp] view plaincopy
     
    1. namespace WpfApplication1 {  
    2.     public class Window1ViewModel {  
    3.   
    4.         public ICommand ButtonCommand {  
    5.             get {  
    6.                 return new DelegateCommand<string>((str) => {  
    7.                     MessageBox.Show("Button's parameter:"+str);  
    8.                 });  
    9.             }  
    10.         }  
    11.   
    12.         public ICommand ButtonCommand2 {  
    13.             get {  
    14.                 return new DelegateCommand<Button>((button) => {  
    15.                     button.Content = "Clicked";  
    16.                     MessageBox.Show("Button");  
    17.                 });  
    18.             }  
    19.         }  
    20.     }  
    21. }  


    这样就可以在委托中获取Button对象了。但是MVVM本身比建议ViewModel操作View。

  • 相关阅读:
    Unity3D Editor模式下批量修改prefab
    3D touch在Unity3D中的使用
    Unity中的协程是什么?
    Unity3D脚本调用Objective C代码实现游戏内购买
    WindowsPhone8拍照功能实现简介
    WindowsPhone App如何扩展能够使用的内存
    SVN 提交代码时提示文件已经存在解决办法
    iOS检查App新版本并更新新版本
    iOS存储数据字典到沙盒
    统计整个Xcode工程代码行数
  • 原文地址:https://www.cnblogs.com/newcoder/p/4800721.html
Copyright © 2011-2022 走看看