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.