Tags: attribute, control, create, element, event, handler, ltcontentcontrol, microsoft, msdn, requires, resourcedictionary, root, software, studio, visual, wpf, xclass, xclassquotskinwindowquot
How to : ResourceDictionary' root element requires a x:Class attribute to support event handler
On Microsoft » Microsoft Visual Studio
2,740 words with 3 Comments; publish: Thu, 27 Dec 2007 13:21:00 GMT; (300109.01, « »)
Hi,
I have create a new WPF control like this :
<ContentControl x:Class="SkinWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="279" Width="365"
>
<ContentControl.Template>
<ControlTemplate TargetType="{x:Type ContentControl}">
<StackPanel>
<Button Name="button1"/>
<Button Name="button2"/>
<Button Name="button3"/>
</StackPanel>
<Grid>
<Line MouseDown="OnSizeNorth" Name="lnSizeNorth" .../>
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
I would like to be able to change the style of this control by using a "ResourceDictionary", but if I create a ResourceDictionary and set a new template for this control I have the following error message :
"'ResourceDictionary' root element requires a x:Class attribute to support event handlers in the XAML file. Either remove the event handler for the MouseDown event, or add a x:Class attribute to the root element."
In the resource dictionary I redefine this:
<Line MouseDown="OnSizeNorth" Name="lnSizeNorth" .../>
where we have a 'named' line and a event !
To summarize:
I create a control that has sub-elements and sub-controls. On the class file (.cs) I must define some behaviors on theses sub-elements/controls. It work for the default style, but if I want a "custom template" to redefine the style, how can I do this ?
In this control I must always have this kind of line... I use the line in the class file to define some behaviors on it. So can you tell me how can I do this ?
Thanks for your help
http://visual-studio.itags.org/q_visual-studio_43689.html
All Comments
Leave a comment...
- 3 Comments

- I'm not sure I understand the problem completely. Why can't you add the x:Class element and a codebehind file to the ResourceDictionary?#1; Wed, 05 Sep 2007 00:26:00 GMT

- When you create custom control or style the existing controls, the best practice is that you shouldn't add event handler in Xaml, instead you should add event handling code through procedural code, in particular, you should register event handlers in the OnApplyTemplate() method.
Sheva
#2; Wed, 05 Sep 2007 00:27:00 GMT

- Thanks...
It is what I have do, it work fine now..
Thanks again ;-)
#3; Wed, 05 Sep 2007 00:28:00 GMT