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

  • SEARCH
  • Home
  • 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 8866023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:40:28+00:00 2026-06-14T16:40:28+00:00

The following are two xaml snippets where the sole difference is that one example

  • 0

The following are two xaml snippets where the sole difference is that one example directly populates the window’s visual tree and DataContext while the other contructs the same same window by applying data template.

Visual Tree Content / DataContext

<Window>
    <Window.DataContext>
        <local:MyType />
    </Window.DataContext>

    <DockPanel>
        <DockPanel.CommandBindings>
            <CommandBinding Command="ApplicationCommands.New"
                            CanExecute="OnCanExecuteNew"
                            Executed="OnExecuteNew"
                            />
        </DockPanel.CommandBindings>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar>
                <Button Command="ApplicationCommands.New"
                        Content="New"
                        />
            </ToolBar>
        </ToolBarTray>
        <ContentPresenter Content="{Binding SomeProperty}" />
    </DockPanel>
</Window>

Business Object Content & DataTemplate

<Window>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MyType}">
            <DockPanel>
                <DockPanel.CommandBindings>
                    <CommandBinding Command="ApplicationCommands.New"
                                    CanExecute="OnCanExecuteNew"
                                    Executed="OnExecuteNew"
                                    />
                </DockPanel.CommandBindings>
                <ToolBarTray DockPanel.Dock="Top">
                    <ToolBar>
                        <Button Command="ApplicationCommands.New"
                                Content="New"
                                />
                    </ToolBar>
                </ToolBarTray>
                <ContentPresenter Content="{Binding SomeProperty}" />
            </DockPanel>
        </DataTemplate>
    </Window.Resources>
    <Window.Content>
        <local:MyType />
    </Window.Content>
</Window>

The first example (visual tree content & data context) works as might otherwise be expected while the designer raises a compile time error in the second example: “Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.” Despite the designer error I may still run the application locally where I’ve verified that the routed command handlers are being executed. When trying to run the application on other PC’s the application silently fails at startup leaving an xaml load error log entry in the windows event log. When I remove the command binding from the second snippet the designer error goes away and the application executes both locally and on other PC’s without issue.

May somebody please explain to me the cause of the exception and how I can go about specifying command bindings inside templates.

  • 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-06-14T16:40:29+00:00Added an answer on June 14, 2026 at 4:40 pm

    I can reproduce it with following error in designer (VS2010 SP1 target framework .NET4.0)
    System.Windows.Markup.XamlParseException:
    Failed to create a 'CanExecute' from the text 'OnCanExecuteNew'
    System.ArgumentException:
    Error binding to target method.

    But I can build the application and it works on my local machine.
    I think the designer works here different as the WPF runtime.
    When the template is applied in design time and the event handlers of the CommandBinding get resolved, the resulting visual tree of the template is still not a part of the visual tree of the window. That’s why the handlers can not be resolved.
    As a workaround I would consider following options.
    1) Put CommandBindings in window

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.New"
                        CanExecute="OnCanExecuteNew"
                        Executed="OnExecuteNew"/>
    </Window.CommandBindings>
    

    2) Wrap the content of the data template in UserControl and put the event handlers in it’s codebehind.

    UserControl.xaml

    <UserControl x:Class="WpfApplication1.UserControl1">
        <DockPanel>
            <DockPanel.CommandBindings>
                <CommandBinding Command="ApplicationCommands.New"
                                CanExecute="OnCanExecuteNew"
                                Executed="OnExecuteNew"/>
            </DockPanel.CommandBindings>
            <ToolBarTray DockPanel.Dock="Top">
                <ToolBar>
                    <Button Command="ApplicationCommands.New" Content="New"/>
                </ToolBar>
            </ToolBarTray>
            <ContentPresenter Content="{Binding SomeProperty}" />
        </DockPanel>
    </UserControl>
    

    Window.xaml

    <DataTemplate DataType="{x:Type local:MyType}">
        <local:UserControl1/>
    </DataTemplate>
    

    3) Don’t use CommandBindings at all and put your command object(s) in view model (MVVM).

    <Button Command="{Binding NewCommand}" Content="New"/>
    

    As a general rule I recommend to avoid tight coupling of data template and code behind. Data template should be something you take and put in resource dictionary.

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

Sidebar

Related Questions

I have a Window that has two primary areas. One is a TextBox inside
In my XAML i m having following two textBlocks <TextBlock Name=tbGeneratedSignature TextWrapping=Wrap Margin=2,2,0,0 Height=auto
I have created two buttons using the following XAML code. <Button x:Name=Button1 Width=100 Content=Button1
I have the following XAML: <Grid x:Name=LayoutRoot Background=White DataContext={Binding Source={StaticResource MyDataKey}}> <TextBox Name=_myId Text={Binding
So for example... in the following user control I have a grid with two
I have a main window which hosts two views (user controls). One is UserControl1
I have two XAML pages. One page (named Page1) has a listbox named strangers
I created new WPF Application witn two windows. MainWindow.xaml Window1.xaml Added one button in
I have the following two buttons in XAML: <Button Content=Previous Margin=10,0,0,10/> <Button Content=Next Margin=0,0,10,10/>
I have following code: <Window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=145 Width=156> <Window.Resources> <DataTemplate x:Key=tabTemplate> <ScrollViewer>

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.