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

Binding difficulty

1 Answer 76 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 10 Jan 2017, 07:35 PM

I have the following xaml for a button in the column header of my gridview.

<telerik:GridViewDataColumn Width="auto" >
    <telerik:GridViewDataColumn.Header>
        <Button Content="Add New" x:Name="cmdAddNew"
            Command="{Binding DataContext.AddCommand,
                                  RelativeSource={RelativeSource FindAncestor,
                                  AncestorType={x:Type UserControl}}}"
                                  Margin="0,5,5,5"/>
    </telerik:GridViewDataColumn.Header>
</telerik:GridViewDataColumn>

 

When I run the app I get the following error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext.AddCommand; DataItem=null; target element is 'Button' (Name='cmdAddNew'); target property is 'Command' (type 'ICommand')

I can't make heads or tails of what is wrong.

The really strange part is if I add a click handler to my view and call:

    var vm = (TechniquesViewModel) this.DataContext;
    vm.AddCommand.Execute(null);

 

Everything works perfect.

What am I missing? I know I shouldn't be putting that code in my view, but I can't make it work with the xaml I have.

Thanks ... Ed

 

 

 

1 Answer, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 11 Jan 2017, 09:10 AM
Hi,

I tried to replicate the issue on my side, following the described approach, but I did not have any relevant results? Can you please have a look at the attached project and tell me whether there are any notable differences between your approach and mine?

Regards,
Ivan Ivanov
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 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
Ivan Ivanov
Telerik team
Share this question
or