This question already has an answer here:
I get this error:
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''
on this Binding:
<DataGridTemplateColumn Visibility="{Binding DataContext.IsVisible, RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Converter={StaticResource BooleanToVisibilityConverter}}">
The ViewModel is sitting as DataContext in UserControl. The DataContext of the DataGrid (sitting in UserControl) is property within the ViewModel, in ViewModel I have a variable that says whether to show a certain line or not, its binding fails, why?
Here my property :
private bool _isVisible=false;
public bool IsVisible
{
get { return _isVisible; }
set
{
_isVisible= value;
NotifyPropertyChanged("IsVisible");
}
}
When it comes to the function: NotifyPropertyChanged the PropertyChanged event null - mean he failed to register for the binding.
It should be noted that I have more bindings to ViewModel in such a way that works, here is an example:
Command="{Binding DataContext.Cmd, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
DataGridTemplateColumn
is not part of the visual or logical tree, and therefore has no binding ancestor (or any ancestor) so theRelativeSource
doesn't work.Instead you have to give the binding the source explicitly.
And the binding proxy.
Credits.