Skip to content

Instantly share code, notes, and snippets.

@ryanbarrett
Created April 24, 2012 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbarrett/2484167 to your computer and use it in GitHub Desktop.
Save ryanbarrett/2484167 to your computer and use it in GitHub Desktop.
Update View from View Model
<Window x:Class="_0message.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">
<Grid>
<TextBox HorizontalAlignment="Left" Margin="12,266,0,0" Name="tbSendMessageText" VerticalAlignment="Top" Width="392" Height="auto" AcceptsReturn="False" Text="{Binding SendText, Mode=TwoWay}" />
<TextBlock Height="248" HorizontalAlignment="Left" Margin="12,12,0,0" Name="tbReceiveMessage" VerticalAlignment="Top" Width="479" Text="{Binding ReceivedText, Mode=TwoWay}" />
<Button Content="Send" Height="23" HorizontalAlignment="Left" Margin="416,266,0,0" Name="bSend" VerticalAlignment="Top" Width="75" Command="{Binding Send}" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
namespace _0message
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void SendMessage()
{
_0Logic logic = new _0Logic();
logic.Send(SendText);
}
protected void OnNotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
private ICommand _sendCommand;
public ICommand Send
{
get
{
if (_sendCommand == null)
{
_sendCommand = new Command(() => SendMessage());
}
return _sendCommand;
}
}
private string _sendtext = String.Empty;
public string SendText
{
get
{
return _sendtext;
}
set
{
if (_sendtext != value)
{
_sendtext = value;
OnNotifyPropertyChanged("SendText");
}
}
}
}
public class Command : ICommand
{
private Action _action = null;
public Command(Action action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment