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

6 posts, 0 answers
  1. Jeff
    Jeff avatar
    64 posts
    Member since:
    Dec 2011

    Posted 31 Jul Link to this post

    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?

  2. Jeff
    Jeff avatar
    64 posts
    Member since:
    Dec 2011

    Posted 02 Aug Link to this post

    Any ideas? I'm still stuck.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    280 posts

    Posted 03 Aug Link to this post

    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.
  5. Jeff
    Jeff avatar
    100 posts
    Member since:
    Apr 2008

    Posted 03 Aug in reply to Stefan Nenchev Link to this post

    It very well might.

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

    Thanks.

  6. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    280 posts

    Posted 04 Aug Link to this post

    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.
  7. Jeff
    Jeff avatar
    100 posts
    Member since:
    Apr 2008

    Posted 04 Aug in reply to Stefan Nenchev Link to this post

    Thank you.
Back to Top
UI for WPF is Visual Studio 2017 Ready