Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MyView.xaml.cs
- public partial class MyView : UserControl, IViewFor<MyViewModel>
- {
- public MyView()
- {
- InitializeComponent();
- ViewModel = new MyViewModel();
- _otherViewModel = new MyViewModel();
- this.Bind(_otherViewModel, vm => vm.StrProp, v => v.textBox.Text);
- }
- private readonly MyViewModel _otherViewModel;
- public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
- "ViewModel", typeof (MyViewModel), typeof (MyView), new PropertyMetadata(default(MyViewModel)));
- public MyViewModel ViewModel
- {
- get { return (MyViewModel) GetValue(ViewModelProperty); }
- set { SetValue(ViewModelProperty, value); }
- }
- object IViewFor.ViewModel
- {
- get { return ViewModel; }
- set { ViewModel = (MyViewModel) value; }
- }
- private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("ViewModel.StrProp = " + ViewModel.StrProp +
- "\nOtherViewModel.StrProp = " + _otherViewModel.StrProp);
- }
- }
- public class MyViewModel : ReactiveObject
- {
- private string _strProp;
- public string StrProp
- {
- get { return _strProp; }
- set { this.RaiseAndSetIfChanged(ref _strProp, value); }
- }
- }
- // MyView.xaml
- <UserControl x:Class="WpfApplication1.MyView"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="300">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition />
- <RowDefinition />
- </Grid.RowDefinitions>
- <TextBox x:Name="textBox"></TextBox>
- <Button Grid.Row="1" Click="ButtonBase_OnClick">Show ViewModel and OtherViewModel</Button>
- </Grid>
- </UserControl>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement