Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active December 17, 2015 06:08
Show Gist options
  • Save JohanLarsson/5562611 to your computer and use it in GitHub Desktop.
Save JohanLarsson/5562611 to your computer and use it in GitHub Desktop.
public class DummyViewModel : INotifyPropertyChanged
{
public DummyViewModel()
{
Values= new ObservableCollection<Reading>();
_timer = new Timer((o) =>
{
this.TextFromSerialPort = DateTime.Now.ToString();
App.Current.Dispatcher.Invoke(()=>this.Values.Add(new Reading(DateTime.Now,"FakeValue " + DateTime.Now.ToString())));
}, null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0.1));
}
private Timer _timer;
private string _textFromSerialPort;
public string TextFromSerialPort
{
get { return this._textFromSerialPort; }
set
{
if (value == this._textFromSerialPort) return;
this._textFromSerialPort +=Environment.NewLine + value;
this.OnPropertyChanged();
}
}
public ObservableCollection<Reading> Values { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Reading
{
public Reading(DateTime time, string value)
{
this.Time = time;
this.Value = value;
}
public DateTime Time { get; set; }
public string Value { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new DummyViewModel();
}
}
<Window x:Class="SerialPortDummy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:serialPortDummy="clr-namespace:SerialPortDummy"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance serialPortDummy:DummyViewModel, IsDesignTimeCreatable=True}"
Title="MainWindow" Height="600" Width="800">
<DockPanel>
<DataGrid ItemsSource="{Binding Values}" DockPanel.Dock="Top" Height="400"></DataGrid>
<TextBox Text="{Binding TextFromSerialPort}"></TextBox>
</DockPanel>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment