1

I try to bind a value in a data grid via the resources of a data template and I get the warning message "Cannot find governing FrameworkElement or FrameworkContentElement for target element. [...]".

Example Dependency Object

public class ExampleDO : DependencyObject, INotifyPropertyChanged
{
    //Implementation for Interface is given and called in Method RaisePropertyChange
    ...

    public static readonly DependencyProperty TestProperty = DependencyProperty.Register("Test", typeof(object), typeof(ExampleDO), new PropertyMetadata(default(object)));

    public Object Test 
    {
         get { return GetValue(TestProperty); }
         set { SetValue(TestProperty, value); }
    }

    private string _testValue = "Hello World";

    public string TestValue
    {
         get { return _testValue; }
         set { _testValue = value; RaisePropertyChange(); }
    }
}

... and the implementation in XAML:

<DataGrid ...>
    <DataGrid.Columns>
        <DataGridTemplateColumn>
             <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                      <DataTemplate.Resources>
                           <local:ExampleDO x:Key="DO" Test="{Binding}"/>
                      </DataTemplate.Resources>
                      <TextBlock Text="{Binding Source={StaticResource DO}, Path=Test}"/>
                  </DataTemplate>
             </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

Please note that this is a very stupid example. Normally I would set values from the dependency property into the other properties in the DO object. And note that I wrote the text directly, this is not a copy of an existing code, so it can may contains mistakes.

What is the binding problem for C# / WPF?

Thanks @all!

user1770962
  • 211
  • 1
  • 2
  • 11
  • Having run into this myself, I have sympathy for the frustration it causes. But, there are _numerous_ examples of your question already on Stack Overflow. The marked duplicate is basically the same scenario you're dealing with. The short answer is that WPF requires a context involving a `FrameworkElement` (or `FrameworkContentElement`) to process the `{Binding}` extension and configure the binding. In a template, this doesn't exist when you try to set the `Source` the way you are here. – Peter Duniho Apr 01 '17 at 07:31
  • You should focus your question less on this specific error, and more on whatever broader goal it is you're trying to achieve. That way the question won't be so much of a duplicate and/or [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Peter Duniho Apr 01 '17 at 07:31
  • Bind directly to `Text` property and use `Converter`. – AnjumSKhan Apr 01 '17 at 07:31
  • Thanks for answers. I try to extend instead from a dependency object from FrameworkElement. In this case the warning is not shown but the binding not work correctly, it means no value is set to the target property. Only if I show the framework element in the template the binding will successfully. – user1770962 Apr 01 '17 at 08:25

0 Answers0