Notes on how WPF obtains view Control objects in ViewModel in MVVM mode

Source: Internet
Author: User

Transfer from http://blog.csdn.net/qing2005/article/details/6601199
http://blog.csdn.net/qing2005/article/details/6601475
command binding is easily implemented in MVVM (ii) Pass command arguments

Property bar to set the. The statement should be commandparameter= "{Binding elementname= control name}"

If we need to pass parameters in command, the implementation is simple. Delegatecommand also has a delegatecommand<t> version that can pass a parameter of type T.

A button binding of 1.View, where CommandParameter defines a parameter of "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="> "
  6. <window.datacontext>
  7. <vm:window1viewmodel />
  8. </window.datacontext>
  9. <Grid>
  10.         <Button < span class= "attribute" >content= "button"  height= "  horizontalalignment= margin=" 34,20,0,0 "  name= "button1"   Verticalalignment= "Top"  width= "109" &NBSP;&NBSP;
  11. command="{Binding Buttoncommand}"
  12. commandparameter="/> "
  13. </Grid>
  14. </Window>


2.ViewModel define commands, note delegate parameters

[CSharp]View Plaincopy
  1. Namespace WpfApplication1 {
  2. public class Window1viewmodel {
  3. Public ICommand Buttoncommand {
  4. get {
  5. return new delegatecommand<string> ((str) + = {
  6. MessageBox.Show ("button ' s parameter:" +STR);
  7. });
  8. }
  9. }
  10. }
  11. }


And, in special cases, we can pass the control object as a parameter to ViewModel, noting that {binding relativesource={x:static relativesource.self}} is the meaning of binding itself (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="> "
  6. <window.datacontext>
  7. <vm:window1viewmodel />
  8. </window.datacontext>
  9. <Grid>
  10. <button content="button" height="horizontalalignment=" "left " margin= "34,20,0,0" name="button1" verticalalignment="Top" width="109 "
  11. command="{Binding Buttoncommand}"
  12. commandparameter="/> "
  13.         < Button content= "button"   height= "all"   "left"  margin=" 34,85,0,0 " name=" Button2 " < span class= "attribute" >verticalalignment= "Top"  width = "109" &NBSP;&NBSP;
  14. command="{Binding ButtonCommand2}"
  15. commandparameter="{Binding relativesource={x:static Relativesource.self}}"/>
  16. </Grid>
  17. </Window>

Look at ViewModel again.

[CSharp]View Plaincopy
  1. Namespace WpfApplication1 {
  2. public class Window1viewmodel {
  3. Public ICommand Buttoncommand {
  4. get {
  5. return new delegatecommand<string> ((str) + = {
  6. MessageBox.Show ("button ' s parameter:" +STR);
  7. });
  8. }
  9. }
  10. Public ICommand ButtonCommand2 {
  11. get {
  12. return new delegatecommand<button> (Button) = {
  13. button.  Content = "Clicked";
  14. MessageBox.Show ("button");
  15. });
  16. }
  17. }
  18. }
  19. }


This allows you to get the button object in the delegate. But MVVM itself is better than suggesting ViewModel operation view.

Notes on how WPF obtains view Control objects in ViewModel in MVVM mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.