2

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.

Chris Kerekes
  • 1,116
  • 8
  • 27
  • you'll have to explain " I move DataContext from the previous snippet directly into the contents of the window and move the rest of the template into a DataTemplate" – user1834059 Nov 20 '12 at 02:51
  • That should have read `[...] move the window contents into a DataTemplate and move the DataContext into the window contents`. That may still be confusing so I've restructed the question to better illustrate the two scenarios. – Chris Kerekes Nov 20 '12 at 06:02

1 Answers1

2

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.

Lubo
  • 754
  • 6
  • 8
  • The data template was in a resource file (until I encountered the issue described here). I'm also OK with placing the command bindings inside the template as the actions associated with the command bindings are limited to the templated data. – Chris Kerekes Nov 21 '12 at 16:11