Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6727149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:00:49+00:00 2026-05-26T10:00:49+00:00

I have a custom control that has an ICommand dependency property. I would like

  • 0

I have a custom control that has an ICommand dependency property. I would like to bind to this property from inside a DataTemplate. To make things clearer here’s my code:

DataForm class:

public class DataForm : Form
{
    #region Properties

    public ICommand HideForm
    {
        get { return (ICommand)GetValue(HideFormProperty); }
        set { SetValue(HideFormProperty, value); }
    }

    public static readonly DependencyProperty HideFormProperty =
        DependencyProperty.Register("HideForm", typeof(ICommand), typeof(DataForm), new PropertyMetadata(null));


    #endregion

    #region Ctors
    public DataForm(Guid formId, String title)
        : base(formId, title)
    {
        this.Template = Application.Current.Resources["DataFormDefaultTemplate"] as ControlTemplate;
    }
    public DataForm()
        : this(Guid.NewGuid(), "bla")
    {
    }
    #endregion

    #region Methods
    public override void OnApplyTemplate()
    {
        if (HeaderTemplate == null)
        {
            HeaderTemplate = Application.Current.Resources["DefaultFormHeaderTemplate"] as DataTemplate;
        }
        base.OnApplyTemplate();
    }
    #endregion
}

ControlTemplate of the DataForm:

<ControlTemplate x:Name="DataFormDefaultTemplate" TargetType="my:DataForm">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ContentPresenter Grid.Row="0" ContentTemplate="{TemplateBinding HeaderTemplate}"></ContentPresenter>
        <ContentPresenter Grid.Row="1" ContentTemplate="{TemplateBinding BodyTemplate}"></ContentPresenter>
        <ContentPresenter Grid.Row="2" ContentTemplate="{TemplateBinding FooterTemplate}"></ContentPresenter>
    </Grid>
</ControlTemplate>

The DataTemplate from which I want to bind to the HideForm command:

<DataTemplate x:Name="DefaultFormHeaderTemplate">
    <Grid HorizontalAlignment="Stretch">
        <my:TextField FieldViewStyle="{StaticResource formLabelStyle}" Value="Form Title" Mode="View"></my:TextField>
        <my:ImageButtonField ImagePath="../Images/Popup_Close.png" 
                         CommandToExecute="{Binding HideForm}" 
                             HorizontalAlignment="Right" Width="8" Height="8"></my:ImageButtonField>
    </Grid>
</DataTemplate>

Note: CommandToExecute does work properly on the ImageButtonField. I’ve already tested it.

Code to instantiate the DataForm:

    DataForm form = new DataForm();
    form.BodyTemplate = Application.Current.Resources["DataFormBodyTemplate"] as DataTemplate;
    form.FooterTemplate = Application.Current.Resources["DataFormFooterTemplate"] as DataTemplate;

    form.HideForm = new DelegateCommand(CloseIt);

How do I get this to work? Basically, the perfect scenario is to be able to bind to this command (and other properties) in the data template from the ViewModel without having to add that property (HideForm in this case) to the DataForm class. Shouldn’t the DataContext be inherited and – theoretically – what I’m saying should work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:00:50+00:00Added an answer on May 26, 2026 at 10:00 am

    Turned out that there is no way to get this done with a DataTemplate unless you use a Binding Proxy. The other way around is to use a ControlTemplate instead and bind to the Template property of a ContentControl instead of using a ContentPresenter

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I have a custom control in an aspx page that has a property named
I have a custom control that has a property that is an ObservableCollection of
I have a custom control that is derived from ListView . This list view
I have a custom control that has an Items property. I Have applied an
I have a custom control that has the following prototype. Type.registerNamespace('Demo'); Demo.CustomTextBox = function(element)
I have a Custom Control that has multiple textbox fields and a checkbox contained
I have a custom control that inherits from WebControl and implements IValidator, but I
I have a custom control that exposes a property. When I set it using
I have a custom control that shows a value obtained from the database (the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.