user17753
user17753

Reputation: 3161

How to extend xaml objects?

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?

Upvotes: 1

Views: 324

Answers (1)

Dan Puzey
Dan Puzey

Reputation: 34200

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.)

Upvotes: 2

Related Questions