Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top