title: Add dependency property to a WPF behaviour
body: <p>I have the following behavior that sets the color formatting of a GridControl which works fine if I set a static ColorScaleFormat. I however need to databind it to my view model as the colorscale format depends on the model data.</p>
  
  <p>Anyway to do so I need to make it a DependencyProperty as done below. The issue is I get the following error at run time:
  A 'Binding' cannot be set on the 'ColorScaleFormat' property of type 'DynamicConditionBehavior'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.</p>
  
  <pre><code>public class DynamicConditionBehavior : Behavior&lt;GridControl&gt;
  {
      GridControl Grid =&gt; AssociatedObject;
  
      protected override void OnAttached()
      {
          base.OnAttached();
          Grid.ItemsSourceChanged += OnItemsSourceChanged;
      }
  
      protected override void OnDetaching()
      {
          Grid.ItemsSourceChanged -= OnItemsSourceChanged;
          base.OnDetaching();
      }
  
      public ColorScaleFormat ColorScaleFormat {
          get { return (ColorScaleFormat) GetValue(ColorScaleFormatProperty); }
          set { SetValue(ColorScaleFormatProperty, value);}
      }
      public static ColorScaleFormat defaultColorScaleFormat = new ColorScaleFormat
      {
          ColorMin = (Color)ColorConverter.ConvertFromString("#FFF8696B"),
          ColorMiddle = (Color)ColorConverter.ConvertFromString("#FFFFEB84"),
          ColorMax = (Color)ColorConverter.ConvertFromString("#FF63BE7B")
      };
  
      public static readonly DependencyProperty ColorScaleFormatProperty = DependencyProperty.Register(
          "ColorScaleFormat", typeof(ColorScaleFormat), typeof(ColorScaleFormatProperty), new FrameworkPropertyMetadata(defaultColorScaleFormat));
  
      ....
  
      private void OnItemsSourceChanged(object sender, EventArgs e)
      {
          var view = Grid.View as TableView;
          if (view == null) return;
  
          view.FormatConditions.Clear();
          foreach (var col in Grid.Columns)
          {
              view.FormatConditions.Add(new ColorScaleFormatCondition
              {
                  MinValue = 0,
                  MaxValue = 20,
                  FieldName = col.FieldName,
                  Format = ColorScaleFormat,  
              });
          }
      }
  }
  </code></pre>
  
  <p>My view model is as follows:</p>
  
  <pre><code>[POCOViewModel]
  public class Table2DViewModel
  {
      public DataTable ItemsTable { get; set; }
      public ColorScaleFormat ColorScaleFormat { get; set; }
      public static Table2DViewModel Create(Table2D table2D)
      {
          var factory = ViewModelSource.Factory((Table2D table) =&gt; new Table2DViewModel(table));
          return factory(table2D);
      }
  }
  </code></pre>
  
  <p>and my Table2DView XAML code:</p>
  
  <pre><code>&lt;dxg:GridControl ItemsSource="{Binding ItemsTable}"
               AutoGenerateColumns="AddNew"
               EnableSmartColumnsGeneration="True"&gt;
  &lt;!--DesignTimeDataObjectType="{x:Type ViewModels:RowData}"--&gt;
  
      &lt;dxmvvm:Interaction.Behaviors &gt;
          &lt;behaviors:DynamicConditionBehavior ColorScaleFormat="{Binding ColorScaleFormat, Mode=OneWayToSource}" /&gt;
          &lt;/dxmvvm:Interaction.Behaviors&gt;
          &lt;dxg:GridControl.View&gt;
              &lt;dxg:TableView ShowGroupPanel="False"
                         AllowPerPixelScrolling="True"/&gt;
          &lt;/dxg:GridControl.View&gt;
      &lt;/dxg:GridControl&gt;
  </code></pre>
  
  <p>If I change the following line in behavior</p>
  
  <pre><code>Format = ColorScaleFormat
  </code></pre>
  
  <p>To</p>
  
  <pre><code>Format = defaultColorScaleFormat
  </code></pre>
  
  <p>And remove the databinding from the XAML everything works, however I want to figure out how to databind ColorScaleFormat to my ViewModel so I can change it when the data changes by creating this a property.</p>
  
  <p>How can I make my DynamicConditionBehavior implement DependencyObject and hence allow the ColorScaleFormat to be databinded?</p>
  
  <p>edit: I found this class which may help however I'm not sure if it is required in my case
  <a href="http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/" rel="nofollow noreferrer">http://blog.falafel.com/adding-a-dependency-property-to-a-class-that-is-not-a-dependency-object/</a></p>
  
  <p>edit2: In the time being I have worked around the problem by making an ITable2DView interface, passing a reference of the View back to the ViewModel. The View model then calls a function called SetColourFormatter and passes the variables back to the Behaviour which works ok. I'm still curious if the above is possible though. It currently appears as if it is not.</p>