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。

  • 相关阅读:
    有固态硬盘的电脑还是不流畅?这些值得了解
    一名神舟笔记本电脑用户的内心独白
    验证码无法显示:Could not initialize class sun.awt.X11GraphicsEnvironment 解决方案
    nginx 负载均衡时,一台tomcat宕机时的问题 自动切换
    tomcat结合nginx使用小结
    金九银十面试突击,卧底去阿里、京东、美团、腾讯带回来的面试题(内含答案)
    干货分享 ▏Jmeter-场景设置/运行/参数化访问地址/【一次性打包呈现】
    各类APP功能测试用例/设计方法/数据库和日记分析——【范本】
    软件测试这些坑,千万不要踩!
    技术面试中,遇到不会回答的问题怎么破?来,教3招!
  • 原文地址:https://www.cnblogs.com/newcoder/p/4800721.html
Copyright © 2011-2022 走看看