title: WPF Binding to behavior property is not working
body: <p>I have a custom behavior that take a boolean as parameter. I'm trying to bind it to a property in my model but I can't get it to work. (I'm using DevExpress)</p>
  
  <pre><code>&lt;dxg:GridControl
          x:Name="dgArticles"
          Grid.Row="1"
          AutoGenerateColumns="None"
          ItemsSource="{Binding Path=Articles, Mode=TwoWay}"&gt;
  
          &lt;dxmvvm:Interaction.Behaviors&gt;
              &lt;util:ExpandAllBehavior HasLotPartie="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CahierDesCharges.HasLotPartie }" /&gt;
          &lt;/dxmvvm:Interaction.Behaviors&gt;
  
  
      &lt;/dxg:GridControl
  </code></pre>
  
  <p>This is a WPF UserControl and the DataContext is defined in code-behind as follow:</p>
  
  <pre><code>public partial class GridView : UserControl
  {
      public GridView(ArticleViewModel a)
      {
          InitializeComponent();
          this.DataContext = a;          
      }        
  }
  </code></pre>
  
  <p>My ViewModel:</p>
  
  <pre><code>public class ArticleViewModel : INotifyPropertyChanged
  {
  
      private Cahier cahierDesCharges;
      public Cahier CahierDesCharges { get { return cahierDesCharges; } set { } }
  
      private ObservableCollection&lt;Article&gt; articles;
      public ObservableCollection&lt;Article&gt; Articles
      {
          get { return articles; }
          set { articles = value; OnPropertyChanged("articles"); }
      }
  
      public ArticleViewModel() { }
  
      public ArticleViewModel(Cahier c)
      {
          this.cahierDesCharges = c;
          this.articles = CahierDesCharges.Articles;
      }
  
      private void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this,
                  new System.ComponentModel.PropertyChangedEventArgs(propertyName));
  
      }
  }
  </code></pre>
  
  <p>And the CahierDesCharges Class:</p>
  
  <pre><code> public class Cahier : INotifyPropertyChanged
  {
      public bool HasLotPartie { get; set; }
  
      private ObservableCollection&lt;Article&gt; articles;
      public ObservableCollection&lt;Article&gt; Articles
      {
          get { return articles; }
          set { articles = value; OnPropertyChanged("articles"); }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public Cahier() { }
      private void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this,
                  new System.ComponentModel.PropertyChangedEventArgs(propertyName));
          Console.WriteLine(propertyName);
      }
  }
  </code></pre>
  
  <p>The Behavior:</p>
  
  <pre><code>public class ExpandAllBehavior : Behavior&lt;GridControl&gt;
  {
      public static readonly DependencyProperty HasLotPartieProperty =
      DependencyProperty.Register("HasLotPartie", typeof(Boolean), typeof(ExpandAllBehavior));
      public bool HasLotPartie
      {
          get { return (bool)GetValue(HasLotPartieProperty); }
          set { SetValue(HasLotPartieProperty, value); }
      }
  
      protected override void OnAttached()
      {
          if (HasLotPartie)
          {
              base.OnAttached();
  
              this.AssociatedObject.CustomRowFilter += AssociatedObject_Loaded;
          }
  
      }
  
      void AssociatedObject_Loaded(object sender, EventArgs e)
      {
          int dataRowCount = AssociatedObject.VisibleRowCount;
          for (int rowHandle = 0; rowHandle &lt; dataRowCount; rowHandle++)
              AssociatedObject.ExpandMasterRow(rowHandle);
  
  
      }
  
  
  
      protected override void OnDetaching()
      {
          base.OnDetaching();
      }
  }
  </code></pre>
  
  <p>I have used the line <code>"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CahierDesCharges.HasLotPartie }"</code> at a different place and I know it's working. In the following example it's working perfectly fine:</p>
  
  <pre><code> &lt;dxg:GridControl.View&gt;
              &lt;dxg:TableView
                  AllowScrollAnimation="True"
                  EnableImmediatePosting="True"
                  IsDetailButtonVisibleBinding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.CahierDesCharges.HasLotPartie }"
                  Name="view"
                  ShowGroupedColumns="False"
                  ShowGroupPanel="False"
                  UseLightweightTemplates="None" /&gt;
          &lt;/dxg:GridControl.View&gt;
  </code></pre>
  
  <p>The behavior works fine if I manually set the value to true: <code>&lt;util:ExpandAllBehavior HasLotPartie="True" /&gt;</code> So I thought that it might be that the DadaContext is not inherited and I use the trick explain by Thomas Levesque on his blog <a href="https://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/" rel="nofollow noreferrer">here</a> and this is what I got:</p>
  
  <pre><code>&lt;dxg:GridControl.Resources&gt;             
              &lt;util:BindingProxy x:Key="proxy" Data="{Binding}" /&gt;
          &lt;/dxg:GridControl.Resources&gt;
          &lt;dxmvvm:Interaction.Behaviors&gt;
              &lt;util:ExpandAllBehavior HasLotPartie="{Binding Data.CahierDesCharges.HasLotPartie, Source={StaticResource proxy}}" /&gt;
          &lt;/dxmvvm:Interaction.Behaviors&gt;
  </code></pre>
  
  <p>I've used this trick several time and I know it works too but not in this scenario. Right now I'm stuck with this after several hours of searching so any help is very welcome.</p>
  
  <p>Thank you</p>