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

Error: “This operation is valid only on elements that have this template applied”

5 Answers 341 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 31 Jul 2016, 12:10 PM

I have a C# WPF application that contains a user control:

<UserControl
        x:Name="payrollEntryControl"
        x:Class="MyNamespace.PayrollEntryControl"
        [...]
        >
    [...]
</UserControl>

Within the user control, I have a Telerik RadDataForm:

<telerik:RadDataForm
        x:Name="payrollAddForm"
        CurrentItem="[...]"
        EditTemplate="{StaticResource myEditTemplate}"
        />

 

The template contains a Telerik RadGridView and a Button:

<telerik:RadGridView Grid.Row="0" Grid.Column="0"
        x:Name="workGridView"
        [...]
        ItemsSource="{Binding [...]}"
        >
    <telerik:RadGridView.Columns>
        [...]
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
<Button Grid.Row="1" Grid.Column="0"
        Command="{Binding addWorkCommand, ElementName=payrollEntryControl}"
        >
    Add
</Button>

I want the command to do is call BeginInsert() on workGridView. But I can't seem to get access to workGridView.

My command, so far:

private DelegateCommand addWorkCommand_ = null;
public DelegateCommand addWorkCommand
{
    get
    {
        if (this.addWorkCommand_ == null)
        {
            this.addWorkCommand_ = new DelegateCommand(
                o => addWork(o)
            );
        }
 
        return this.addWorkCommand_;
    }
}
 
private void addWork(object o)
{
    var addForm = this.payrollAddForm;
    var editTemplate = addForm.EditTemplate;
    var workGrid = editTemplate.FindName("workGridView", addForm);
}

 

My problem? When I make the call to editTemplate.FindName(), I get an exception:

This operation is valid only on elements that have this template applied.

I don't understand. I'm getting the template from the form. How can it not be applied?

5 Answers, 1 is accepted

Sort by
0
Jeff
Top achievements
Rank 1
answered on 02 Aug 2016, 05:40 PM
Any ideas? I'm still stuck.
0
Stefan Nenchev
Telerik team
answered on 03 Aug 2016, 02:10 PM
Hi Jeff,

I suggest you pass the workGridView as a CommandParameter and eventually use the RadGridView BeginInsert method:
<Button Grid.Row="1" Grid.Column="0"
        CommandParameter="{Binding ElementName=workGridView}"
        Command="{Binding addWorkCommand}" >
 
....
 
  private void addWork(object o)
        {
            RadGridView grid = o as RadGridView;
            grid.BeginInsert();
        }

Would this work for you?

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Jeff
Top achievements
Rank 1
answered on 03 Aug 2016, 02:44 PM

It very well might.

I'd found a hack using VisualTreeHelper that seems to work, but I'm not really comfortable with it.

Thanks.

0
Stefan Nenchev
Telerik team
answered on 04 Aug 2016, 08:42 AM
Hello Jeff,

I have tested the suggested approach with the CommandParameter and it works as expected. Please give it a try as it seems appropriate for your scenario.

Regards,
Stefan Nenchev
Telerik by Progress
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Jeff
Top achievements
Rank 1
answered on 04 Aug 2016, 02:15 PM
Thank you.
Tags
DataForm
Asked by
Jeff
Top achievements
Rank 1
Answers by
Jeff
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Jeff
Top achievements
Rank 1
Share this question
or