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。

  • 相关阅读:
    让windows cmd也用上linux命令
    数据库工具toad与plsql
    Oracle表&DATA复制
    主流WEB开发语言简单对比
    游戏中的碰撞检测
    LRU算法总结及其C算法实现
    MTU
    多款as3游戏框架
    请问2.3e+71,是不是表示23后面有70个0?
    希望OFFICE有一个小阳台,在阳光斜照的下午,开发人员能喝着茶,在蓝色的天空下讨论问题,这样思绪更为清楚
  • 原文地址:https://www.cnblogs.com/newcoder/p/4800721.html
Copyright © 2011-2022 走看看