筆記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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  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 plain copy
 
  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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM