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!