Sunday, April 3, 2011

trap the click event for a button that is in the resourcedictionary

I am trying to trap the click event for a button that is in the resourcedictionary,

I get a error

WpfApplication1.MyResourceDictionary' does not contain a definition for 'GetTemplateChild' and no extension method 'GetTemplateChild' accepting a first argument of type 'WpfApplication1.MyResourceDictionary' could be found (are you missing a using directive or an assembly reference?)

MyResourceDictionay.xaml.cs looks like this

namespace WindowsApplication1

{

partial class MyResourceDictionary

{

public virtual void OnApplyTemplate()

{

 Button myButton = this.GetTemplateChild("Crap") as Button;

}

}

}

MyResourceDictionay.xaml looks like this

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 x:Class="WindowsApplication1.MyResourceDictionary" 

           x:ClassModifier="public">

<Button x:Name="Crap"  Foreground="White" Background="White">

  <Image Source="Controls\check.png"/>

</Button>

Any help?

From stackoverflow
  • If you are trying to search inside of a resource dictionary, you should be using the function FindName(string), not GetTemplateChild(string).

    Even if you are searching for a child control within the template of the current control, you should use Template.FindName(string, FrameworkElement templatedParent) not GetTemplateChild(string).

    If this doesn't help, please expand you description.

    If you have a code behind file, usually you can add the handler right in the XAML and have it attach to the code behind (even if the control is in a dictionary you can often do this).

    I hope this helps.

0 comments:

Post a Comment