This is a migrated thread and some comments may be shown as answers.

Can't find the right ancestor to bind to

9 Answers 4083 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 16 Jan 2017, 08:37 PM

I have a couple of buttons embedded in a header column of my radgridview that is in a usercontrol that is in a pane of a raddock. I am trying to bind the IsVisible property of the button to a property of my MainWindows' view model. Below is the relevant xaml. See line 7 of the xaml. I can't seem to get the binding right. What's the right way to bind this property to the MainWindow's viewmodel property? Obviously, I'm not seeing it.

Any help would be great!!!

Here is the error I get in my command window of vstudio.:

     System.Windows.Data Error: 4 : Cannot find source for binding with reference      'RelativeSource FindAncestor, AncestorType='System.Windows.Window',      AncestorLevel='1''. BindingExpression:Path=DataContext.EnableRowDetailsButton;      DataItem=null; target element is 'Button' (Name='cmdAddNewSample'); target property is     'IsEnabled' (type 'Boolean')

 

01.<telerik:GridViewDataColumn Width="auto" >
02.    <telerik:GridViewDataColumn.Header>
03. 
04.        <StackPanel Orientation="Horizontal">
05.            <Button Content="Add New" x:Name="cmdAddNewSample"
06.                Click="cmdAddNewSample_Click" Tag="AddOrUploadButton"
07.                IsEnabled="{Binding DataContext.EnableRowDetailsButton,
08.                                RelativeSource={RelativeSource FindAncestor,
09.                                                AncestorType={x:Type Window}}}"
10.                Margin="0,5,5,5"/>
11.            <Button Content="Upload" x:Name="cmdUploadSample" Tag="AddOrUploadButton"
12.                IsEnabled="{Binding DataContext.EnableRowDetailsButton,
13.                                RelativeSource={RelativeSource FindAncestor,
14.                                                AncestorType={x:Type Window}}}"
15.            Command="{Binding DataContext.UploadSampleCommand, Mode=TwoWay,
16.                                RelativeSource={RelativeSource FindAncestor,
17.                                                AncestorType={x:Type UserControl}}}"
18.                Margin="0,5,0,5"/>
19.        </StackPanel>
20.    </telerik:GridViewDataColumn.Header>
21.</telerik:GridViewDataColumn>

 

Thanks ... Ed

 

9 Answers, 1 is accepted

Sort by
0
Stefan Nenchev
Telerik team
answered on 18 Jan 2017, 10:30 AM
Hi Ed,

I have tried reproducing the issue but similar bindings work fine at my end. Please have a look at the attached project. Can you share more information on your actual setup? Would it be possible to provide a sample so we can have a more detailed look?

Regards,
Stefan Nenchev
Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Brad
Top achievements
Rank 1
answered on 25 Apr 2018, 07:49 PM

I ran into exactly the same problem today AND the attached sample produced the errors as well!!!

I downloaded the sample and ran it, without making any changes, and here's what I got:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.CustomCommand; DataItem=null; target element is 'Button' (Name='cmdAddNewSample'); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext.ButtonVisibility; DataItem=null; target element is 'Button' (Name='cmdAddNewSample'); target property is 'IsEnabled' (type 'Boolean')

 

I really need a solution to this!

Brad.

0
Dinko | Tech Support Engineer
Telerik team
answered on 30 Apr 2018, 12:03 PM
Hi Brad,

I have examined the attached project and I was able to get such binding error when I drag the RadPane to a floating state. In this scenario, the RadPane is removed from the main visual tree and add into a ToolWindow. In this case, the DataContext is changed and the binding fails. If this is the case in your main application I would suggest you set the Source of the Binding in the Command and IsEnabled property of the Button. I have modified the project from my colleague Stefan to demonstrate this approach. 

Give it a try and let me know if it works for you.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Brad
Top achievements
Rank 1
answered on 30 Apr 2018, 03:44 PM
  1. This happens for me even if I don't drag the RadPane to a floating state.
  2. Your solution doesn't actually help, since I can't use a static resource to point at my data context.

Brad.

0
Brad
Top achievements
Rank 1
answered on 01 May 2018, 10:22 PM
0
Dinko | Tech Support Engineer
Telerik team
answered on 03 May 2018, 07:29 AM
Hello Brad,

Another approach which you could try is to use IValueConverter and bind to the DataContext of the RadGridView, not the Window. I have modified the project from my previous reply to demonstrate this approach.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Brad
Top achievements
Rank 1
answered on 03 May 2018, 04:54 PM

I had tried that before posting and it didn't work reliably for me.

Brad.

0
Dinko | Tech Support Engineer
Telerik team
answered on 08 May 2018, 08:42 AM
Hi Brad,

One more thing which you could try, in your main application, is to set the binding in code. Check the following code snippet.
public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();
 
    Binding binding = new Binding("CustomCommand");
    binding.Source = this.DataContext;
    this.cmdAddNewSample.SetBinding(Button.CommandProperty, binding);
}

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Ivan Ivanov
Telerik team
answered on 26 Jun 2018, 11:18 AM
Hi Brad,

I do not believe that the design used in the official project fits the description of the scenario. The ButtonVisibility property has False value on initialization, which disables the button and makes the command binding lose any sense. I am sending an updated version of the initial project that uses our latest version, works on my side, and does not seem to log any relevant binding data (warning level and above). Neither the static resource, nor the converter work-arounds are used. Can you please test the attached project?. I also recorded the steps that I followed and the state of the output window. In case that the issue in your original project persists, please confirm whether you are using Prism, or any other MVVM, or deployment framework. If this is the case, we can use a sample project to debug the issue on our side.

Regards,
Ivan Ivanov
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Stefan Nenchev
Telerik team
Brad
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Ivan Ivanov
Telerik team
Share this question
or