title: OxyPlot WPF not working with Button Click
body: <p>I’m having some problems with OxyPlot that I have not been able to resolve through their documentation or other searches.  I’m working on a wpf application that will allow the user to open a .csv with a button-click event, then perform some math and report back some useful information.  I’d like to plot some of the generated data hence OxyPlot.  For some reason I cannot get the plot to populate, when the code that generates it, is within the button click event.  To illustrate here is a smaller example:
  This code works (xaml):</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:oxy="http://oxyplot.org/wpf"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:local="clr-namespace:WpfApplication1"
      mc:Ignorable="d"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Grid&gt;
      &lt;Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/&gt;
      &lt;Grid HorizontalAlignment="Left" Height="255" Margin="20,47,0,0" VerticalAlignment="Top" Width="477"&gt;
          &lt;oxy:PlotView Model="{Binding ScatterModel}"/&gt;
      &lt;/Grid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
         InitializeComponent();
          DataContext = this;
          var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
          var s2 = new LineSeries
          {
              StrokeThickness = 1,
              MarkerSize = 1,
              MarkerStroke = OxyColors.ForestGreen,
              MarkerType = MarkerType.Plus
          };
  
          for (int i = 0; i &lt; 100; i++)
          {
              s2.Points.Add(new DataPoint(i, i));
          }
          tmp.Series.Add(s2);
          this.ScatterModel = tmp;
      }
  
      private void button_Click(object sender, RoutedEventArgs e)
      {
  
      }
      public PlotModel ScatterModel { get; set; }
  </code></pre>
  
  <p>And produces this:
  <a href="https://i.stack.imgur.com/dBX8t.png" rel="nofollow noreferrer">Plot Working</a></p>
  
  <p>But, without changing the xaml, if I copy/paste the code beneath the button click event:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
         InitializeComponent();
  
      }
  
      private void button_Click(object sender, RoutedEventArgs e)
      {
          DataContext = this;
          var tmp = new PlotModel { Title = "Scatter plot", Subtitle = "y = x" };
          var s2 = new LineSeries
          {
              StrokeThickness = 1,
              MarkerSize = 1,
              MarkerStroke = OxyColors.ForestGreen,
              MarkerType = MarkerType.Plus
          };
  
          for (int i = 0; i &lt; 100; i++)
          {
              s2.Points.Add(new DataPoint(i, i));
          }
          tmp.Series.Add(s2);
          this.ScatterModel = tmp;
      }
      public PlotModel ScatterModel { get; set; }
  </code></pre>
  
  <p>The plot never generates: <a href="https://i.stack.imgur.com/EK8HE.png" rel="nofollow noreferrer">Not working</a>:</p>
  
  <p>I’ve tried moving DataContext = this; back up to public MainWindow(), and vice-versa with InitializeComponent(); no change.  I’ve also tried defining</p>
  
  <pre><code>&lt;Window.DataContext&gt;
      &lt;local:MainWindow/&gt;
  &lt;/Window.DataContext&gt;
  </code></pre>
  
  <p>in the xaml but that throws an exception/infinite loop error during build.
  Something simple I fear I'm not getting about OxyPlot implementation?</p>
  
  <p>Thanks!
  CSMDakota</p>