views:

51

answers:

1

I have created a custom user control to use as a base class for some maintenance functions. I would like to be able to wire up some events to handlers defined in the base class. I can do this manually in the code behind but would like to assign them in XAML. Is this not possible?

<src:CustomerMaintenanceControlBase x:Class="ProjectManager.CustomerMaintenanceControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
xmlns:my="clr-namespace:ProjectManager.CustomerServiceReference"
xmlns:src="clr-namespace:ProjectManager"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400"
PrimaryViewSourceName="customerViewSource"
                                Loaded="CustomerMaintenanceControlBase_Loaded"
>

Loaded="CustomerMaintenanceControlBase_Loaded" compiles but fails at runtime. I can wire this up in the code behind but that isn't as much fun. :)

A: 

Why are you trying to code the Loaded event in XAML?

If you are inheriting from CustomerMaintenanceControlBase, then that base class should be able to wire itself.

It might not be as much fun, but requiring the event to be wired in XAML defeats the purpose of putting it in the base class, unless it is "optional." I'm assuming that your method contains logic that should run anytime the derived controls are loaded, so why give the developers a chance to generate issues by forgetting the load event? Why not just put this in your constructor in the base class:

protected CustomerMaintenanceControlBase()
{
   Loaded += CustomerMaintenanceControlBase_Loaded; 
}

And then it will wire for any derived control without additional effort on the part of the developer?

Jeremy Likness
I was only using the 'Loaded' event as an example.Most of the events will be 'optional' like binding click and keypress events.
Dusty Lau