1

If I extend an existing object, like a DataGrid:

    public class CustomDataGrid : DataGrid
    {
        static CustomDataGrid()
        {
            CommandManager.RegisterClassCommandBinding(
                typeof(CustomDataGrid),
                new CommandBinding(ApplicationCommands.Paste,
                    new ExecutedRoutedEventHandler(OnExecutedPaste),
                    new CanExecuteRoutedEventHandler(OnCanExecutePaste)));
        }

...

On the xaml side of things, if I try using a <CustomDataGrid/> I get something like, CustomDataGrid is not supported in a Windows Presentation Foundation (WPF) project. So how do I actually use the extended class on the xaml side?

user17753
  • 3,083
  • 9
  • 35
  • 73
  • Note also that you usually shouldn't be subclassing UI controls, and putting a static constructor on it is just... odd. – BradleyDotNET Mar 03 '15 at 17:53
  • @BradleyDotNET Was just perusing the answers to this question http://stackoverflow.com/questions/4118617/wpf-datagrid-pasting where they did something similar. – user17753 Mar 03 '15 at 18:02

1 Answers1

2

You need to reference the class by namespace. This involves adding a namespace declaration to the top of your Xaml file, and then using that namespace in your control element.

If we assume that your CustomDataGrid is in a namespace called Rhubarb, in the same assembly as the Xaml you're writing , you'd need to add this attribute to the root tag in your Xaml file (alongside the other xmlns attributes):

xmlns:rhubarb="clr-namespace:Rhubarb"

Then, where you declare your grid, use this element instead:

<rhubarb:CustomDataGrid />

If your cod is in a separate (referenced) assembly, you need to modify the namespace declaration thus:

xmlns:rhubarb="clr-namespace:Rhubarb;assembly=NameOfYourAssembly"

(Note that there's no .dll suffix on the assembly name.)

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 1
    It's worth noting that dragging the control from the Toolbox onto the designer or XAML will do this for you. – Mike Eason Mar 03 '15 at 17:51