Skip to content

Instantly share code, notes, and snippets.

@DotNetNerd
Created May 10, 2013 12:32
Show Gist options
  • Save DotNetNerd/5554135 to your computer and use it in GitHub Desktop.
Save DotNetNerd/5554135 to your computer and use it in GitHub Desktop.
Sample of a viewmodel with Reactive UI
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml" xmlns:local="clr-namespace:WpfApplication1.ViewModels">
<Application.Resources>
<local:ApplicationViewModel x:Key="MyApplicationViewModel" />
</Application.Resources>
</Application>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding NewUserViewModel, Source={StaticResource MyApplicationViewModel}}">
<Grid Name="MyGrid">
<TextBox Name="Input" HorizontalAlignment="Left" Height="23" Margin="93,111,0,0" TextWrapping="Wrap"
Text="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
<Label Content="{Binding Path=Password, Mode=TwoWay}" Height="30" Width="100" Margin="93,154,324,136"></Label>
<Button Content="Button" HorizontalAlignment="Left" Margin="218,112,0,0" VerticalAlignment="Top" Width="75" Command="{Binding Path=OkCommand, Mode=TwoWay}" />
</Grid>
</Window>
public class ApplicationViewModel : ReactiveObject
{
private NewUserViewModel _NewUserViewModel;
public NewUserViewModel NewUserViewModel
{
get { return _NewUserViewModel; }
set
{
this.RaiseAndSetIfChanged(x => x.NewUserViewModel, value);
}
}
public ApplicationViewModel()
{
NewUserViewModel = new NewUserViewModel();
}
}
public class NewUserViewModel : ReactiveObject
{
string _Password;
public string Password
{
get { return _Password; }
set
{
this.RaiseAndSetIfChanged(x => x.Password, value);
}
}
public ICommand OkCommand { get; set; }
public NewUserViewModel()
{
var canHitOk = this.WhenAny(x => x.Password, (pass) => (pass.Value != null && pass.Value.Length > 3));
var cmd = new ReactiveCommand(canHitOk);
cmd.Subscribe(_ => MessageBox.Show("Wee: " + Password));
OkCommand = cmd;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment