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 8829729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:54:04+00:00 2026-06-14T07:54:04+00:00

I have a ResourceDictionary , which contains a DataTemplate . Within this DataTemplate’s Resources

  • 0

I have a ResourceDictionary, which contains a DataTemplate. Within this DataTemplate’s Resources I am declaring a CommandBindingCollection. My ResourceDictionary has a codebehind file in which I declare the handlers for Executed/CanExecute.

The issue I have is that when I retreive my CommandBindingCollection from the ResourceDictionary, the Executed/CanExecute handlers are not assigned. Using the debugger I can see that the handlers are null. Why is that and how can I fix it?

ResourceDictionary XAML:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="Test"
                    x:Class="Test.MyResourceDictionary">

    <DataTemplate x:Key="Template"
                  x:Name="Template">
        <DataTemplate.Resources>
            <CommandBindingCollection x:Key="CommandBindings">
                <CommandBinding Command="local:TestCommands.Test"
                        Executed="testExecuted" 
                        CanExecute="testCanExecute" />
            </CommandBindingCollection>
        </DataTemplate.Resources>

        <!-- More stuff here -->

    </DataTemplate>
<ResourceDictionary/>

ResourceDictionary codebehind:

public partial class MyResourceDictionary: ResourceDictionary
{
    public MyResourceDictionary() 
    { 
        InitializeComponent(); 
    } 

    private void testExecuted(object sender, ExecutedRoutedEventArgs e)
    {

    }

    private void testCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {

    }
}

Update

I am using this with AvalonDock, which uses DataTemplateSelector to apply the DataTemplate.

Here is how I load the Template:

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
    if (item is TestViewModel)
    {
        ResourceDictionary res = Application.LoadComponent(new Uri("/MyResourceDictionary.xaml", UriKind.Relative)) as ResourceDictionary;
        DataTemplate template = res["Template"] as DataTemplate;
        if(template != null)
        {
            CommandBindingCollection commandBindings = 
                template.Resources["CommandBindings"] as CommandBindingCollection;

            if(commandBindings != null)
            {
                foreach(var binding in commandBindings)
                {
                     // add commandbinding to the container control
                     // here, using the debugger i can see that the handlers for the commandbinding
                     // are always null (private variables that I can only see using debugger)
                }
            }
            return template;
        }
    }
    return base.SelectTemplate(item, container);
}

If I move the CommandBindingCollection directly into the ResourceDictionary and access it this way:

CommandBindingCollection commandBindings = 
            res["CommandBindings"] as CommandBindingCollection;

Then the handlers are correctly set. I’m wondering why it can’t set the event handler’s delegates when I declare it within the DataTemplate’s Resources.

  • 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-14T07:54:06+00:00Added an answer on June 14, 2026 at 7:54 am

    My problem was related to a bug in the .NET framework, which seems fixed in 4.5.

    There is a hotfix for the problem on 4.0: http://support.microsoft.com/kb/2464222

    In my case, applying the hotfix solved the issue. My guess is that somewhere in the CommandBindingCollection XAML parser, the exception is handled silently.

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

Sidebar

Related Questions

I have a DataTemplate inside a global/shared ResourceDictionary like this which targets a DataType:
I have a parent contentcontrol which displays data via a datatemplate. The datatemplate contains
I have this CustomControl which contains an InkPresenter and an Image . The image
I have 2 Xaml files, one containing a DataTemplate which has a resource definition
I have an application that uses a ResourceDictionary to set the styles, which it
have written this little class, which generates a UUID every time an object of
I have a custom Control which contains the generic.xaml inside the Themes folder. I
I Have a ResourceDictionary Generic.xaml which consists of a set of other resource dictionaries,
I have a resource dictionary inside which I have a context menu: <ResourceDictionary x:Class=MyApp.Components.MyContextMenu
I have this context menu resource: <ResourceDictionary xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <ContextMenu x:Key=FooContextMenu> <ContextMenu.CommandBindings> <CommandBinding Command=Help

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.