Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. // MyView.xaml.cs
  2. public partial class MyView : UserControl, IViewFor<MyViewModel>
  3. {
  4. public MyView()
  5. {
  6. InitializeComponent();
  7. ViewModel = new MyViewModel();
  8. _otherViewModel = new MyViewModel();
  9. this.Bind(_otherViewModel, vm => vm.StrProp, v => v.textBox.Text);
  10. }
  11.  
  12. private readonly MyViewModel _otherViewModel;
  13.  
  14. public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
  15. "ViewModel", typeof (MyViewModel), typeof (MyView), new PropertyMetadata(default(MyViewModel)));
  16.  
  17.  
  18. public MyViewModel ViewModel
  19. {
  20. get { return (MyViewModel) GetValue(ViewModelProperty); }
  21. set { SetValue(ViewModelProperty, value); }
  22. }
  23.  
  24. object IViewFor.ViewModel
  25. {
  26. get { return ViewModel; }
  27. set { ViewModel = (MyViewModel) value; }
  28. }
  29.  
  30. private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
  31. {
  32. MessageBox.Show("ViewModel.StrProp = " + ViewModel.StrProp +
  33. "\nOtherViewModel.StrProp = " + _otherViewModel.StrProp);
  34. }
  35. }
  36.  
  37. public class MyViewModel : ReactiveObject
  38. {
  39. private string _strProp;
  40. public string StrProp
  41. {
  42. get { return _strProp; }
  43. set { this.RaiseAndSetIfChanged(ref _strProp, value); }
  44. }
  45. }
  46.  
  47. // MyView.xaml
  48. <UserControl x:Class="WpfApplication1.MyView"
  49. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  50. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  51. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  52. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  53. mc:Ignorable="d"
  54. d:DesignHeight="300" d:DesignWidth="300">
  55. <Grid>
  56. <Grid.RowDefinitions>
  57. <RowDefinition />
  58. <RowDefinition />
  59. </Grid.RowDefinitions>
  60. <TextBox x:Name="textBox"></TextBox>
  61. <Button Grid.Row="1" Click="ButtonBase_OnClick">Show ViewModel and OtherViewModel</Button>
  62. </Grid>
  63. </UserControl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement