title: WPF MVVM Chart change axes
body: <p>I'm new to WPF and MVVM. I'm struggling to determine the best way to change the view of a chart. That is, initially a chart might have the axes: X - ID, Y - Length, and then after the user changes the view (either via lisbox, radiobutton, etc) the chart would display the information: X - Length, Y - ID, and after a third change by the user it might display new content: X - ID, Y - Quality.</p>
  
  <p>My initial thought was that the best way to do this would be to change the bindings themselves. But I don't know how tell a control in XAML to bind using a Binding object in the ViewModel, or whether it's safe to change that binding in runtime?</p>
  
  <p>Then I thought maybe I could just have a generic Model that has members X and Y and populate them as needed in the viewmodel?</p>
  
  <p>My last thought was that I could have 3 different chart controls and just hide and show them as appropriate.</p>
  
  <p>What is the CORRECT/SUGGESTED way to do this in the MVVM pattern? Any code examples would be greatly appreciated.</p>
  
  <p>Thanks</p>
  
  <p>Here's what I have for the bind to bindings method:</p>
  
  <p>XAML:</p>
  
  <pre><code>        &lt;charting:Chart.Series&gt;
              &lt;charting:BubbleSeries Name="bubbleSeries1"
                                     ClipToBounds="False"
                                     model:MakeDependencyProperty.IndependentValueBinding="{Binding AxisChoice.XBinding}"
                                     model:MakeDependencyProperty.DependentValueBinding="{Binding AxisChoice.YBinding}"
                                     model:MakeDependencyProperty.SizeValueBinding="{Binding AxisChoice.SizeBinding}"
                                     IsSelectionEnabled="True" SelectionChanged="bubbleSeries1_SelectionChanged"
                                     ItemsSource="{Binding Data}"&gt;
              &lt;/charting:BubbleSeries&gt;
          &lt;/charting:Chart.Series&gt;
  
          &lt;ComboBox Height="100" Name="listBox1" Width="120" SelectedItem="{Binding AxisChoice}"&gt;
              &lt;model:AxisGroup XBinding="{Binding Performance}" YBinding="{Binding TotalCount}" SizeBinding="{Binding TotalCount}" Selector.IsSelected="True"/&gt;
              &lt;model:AxisGroup XBinding="{Binding ID}" YBinding="{Binding TotalCount}" SizeBinding="{Binding BadPerformance}"/&gt;
              &lt;model:AxisGroup XBinding="{Binding ID}" YBinding="{Binding BadPerformance}" SizeBinding="{Binding TotalCount}"/&gt;
          &lt;/ComboBox&gt;
  </code></pre>
  
  <p>AxisGroup:</p>
  
  <pre><code>public class AxisGroup : DependencyObject// : FrameworkElement
  {
      public Binding XBinding { get; set; }
      public Binding YBinding { get; set; }
      public Binding SizeBinding { get; set; }
  }
  </code></pre>
  
  <p>DP:</p>
  
  <pre><code>public class MakeDependencyProperty : DependencyObject
  {
      public static Binding GetIndependentValueBinding(DependencyObject obj) { return (Binding)obj.GetValue(IndependentValueBindingProperty); }
      public static void SetIndependentValueBinding(DependencyObject obj, Binding value) { obj.SetValue(IndependentValueBindingProperty, value); }
      public static readonly DependencyProperty IndependentValueBindingProperty =
          DependencyProperty.RegisterAttached("IndependentValueBinding", typeof(Binding), typeof(MakeDependencyProperty), new PropertyMetadata { PropertyChangedCallback = (obj, e) =&gt; { ((BubbleSeries)obj).IndependentValueBinding = (Binding)e.NewValue;}});
  
  
      public static Binding GetDependentValueBinding(DependencyObject obj) { return (Binding)obj.GetValue(DependentValueBindingProperty); }
      public static void SetDependentValueBinding(DependencyObject obj, Binding value) { obj.SetValue(DependentValueBindingProperty, value); }
      public static readonly DependencyProperty DependentValueBindingProperty =
          DependencyProperty.RegisterAttached("DependentValueBinding", typeof(Binding), typeof(MakeDependencyProperty), new PropertyMetadata { PropertyChangedCallback = (obj, e) =&gt; { ((BubbleSeries)obj).DependentValueBinding = (Binding)e.NewValue; } });
  
      public static Binding GetSizeValueBinding(DependencyObject obj) { return (Binding)obj.GetValue(SizeValueBindingProperty); }
      public static void SetSizeValueBinding(DependencyObject obj, Binding value) { obj.SetValue(SizeValueBindingProperty, value); }
      public static readonly DependencyProperty SizeValueBindingProperty =
          DependencyProperty.RegisterAttached("SizeValueBinding", typeof(Binding), typeof(MakeDependencyProperty), new PropertyMetadata { PropertyChangedCallback = (obj, e) =&gt; { ((BubbleSeries)obj).SizeValueBinding = (Binding)e.NewValue; } }); 
  }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>public class BubbleViewModel : BindableObject
  {
      private IEnumerable&lt;SessionPerformanceInfo&gt; data;
      public IEnumerable&lt;SessionPerformanceInfo&gt; Data { ... }
  
      public AxisGroup AxisChoice;
  }
  </code></pre>
  
  <p>This generates the following exception:
  +       $exception  {"Value cannot be null.\r\nParameter name: binding"}    System.Exception {System.ArgumentNullException}</p>
  
  <p>Has something to do with the 4 bindings in teh bubbleSeries. I'm more than likely doing something wrong with binding paths but as I said I'm new to binding and wpf, so any tips would be greatly appreciated.</p>