Cómo acceder a diferentes controles de botón personalizado en la ventana de código

I am new to WPF. i create a custom button in XAML as follows

<Button x:Class="WPFApp.ButtonMainMenuCat"
             d:DesignHeight="150" d:DesignWidth="177">
        <ControlTemplate TargetType="{x:Type Button}">
                <Image Name="Background" Source="/WPFApp;component/Resources/MainScreenMenuBackground.png" Stretch="Fill" />
                <Image Name="MenuNormal" Source="/WPFApp;component/Resources/Audio_Gray.png" Stretch="Fill" Height="62" Width="56" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <Image Name="Pressed" Source="/WPFApp;component/Resources/subMenuNormalButton.png" Stretch="Fill" Visibility="Hidden"/>
                <Image Name="Focused" Source="/WPFApp;component/Resources/buttonFocus.png" Margin="7,7,7,7" Visibility="Hidden"  Stretch="Fill"/>


                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="MenuNormal" Property="Visibility" Value="Hidden"/>
                    <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
                <Trigger Property="IsFocused" Value="True">
                    <Setter TargetName="MenuNormal" Property="Visibility" Value="Hidden"/>
                    <Setter TargetName="Focused" Property="Visibility" Value="Visible"/>


and code behind is as following

namespace WPFApp
    /// <summary>
    /// Interaction logic for ButtonMainMenuCat.xaml
    /// </summary>
    public partial class ButtonMainMenuCat : Button
        public ButtonMainMenuCat()

        public void SetMenuImage(String MenuName)
            var uriSource=new Uri("");
            switch (MenuName.ToLower())
                case "home":
                   uriSource = new Uri(@"/WPFApp;component/Resources/Home_Gray.png", UriKind.Relative);
                case "video":
                    uriSource = new Uri(@"/WPFApp;component/Resources/Vedeo_Gray.png", UriKind.Relative);
                case "audio":
                    uriSource = new Uri(@"/WPFApp;component/Resources/Audio_Gray.png", UriKind.Relative);
                case "services":
                     uriSource = new Uri(@"/WPFApp;component/Resources/Services_Gray.png", UriKind.Relative);
                case "shopping":
                    uriSource = new Uri(@"/WPFApp;component/Resources/Shoppings_Gray.png", UriKind.Relative);
                case "channels":
                     uriSource = new Uri(@"/WPFApp;component/Resources/Channels_Gray.png", UriKind.Relative);
                     uriSource = new Uri(@"/WPFApp;component/Resources/Home_Gray.png", UriKind.Relative);
            WPFApp.ButtonMainMenuCat.MenuNormal.Source = new BitmapImage(uriSource);


now my problem is that i want to change the source of MenuNormal image at run time. but it give me error that Error ButtonMainMenuCat' does not contain a definition for 'MenuNormal'

So please suggest that how can i access the controls in code which is created in XAML

preguntado el 10 de marzo de 12 a las 13:03

2 Respuestas

In order to get the instance of the ´Image´ in your code behind you'll need to use FrameworkElement.GetTemplateChild

Image menuNormal = (Image)GetTemplateChild("MenuNormal");

As an alternative you could bind the source of that image to a property of your button, that is the approach I would use.

Edit: If you use GetTemplateChild then you should also add the TemplatePartAttribute to your class, this is not required, but just a good practice.

respondido 10 mar '12, 14:03

i used the following statement Image menuNormal = (Image)GetTemplateChild("MenuNormal"); but it return null. so give error when set source of image in next statement - Rupesh

@Rupesh, Then you're either using a name that no control in your template has or calling GetTemplateChild before the template has been applied. - Terkel

Necesitas registrar un Propiedad de dependencia on the control you inherit from Button, and then bind the image source to that property.

Then you can set that property in your application code to change the image.

He aquí una introducción:


respondido 10 mar '12, 13:03

That's how I would do it as well, but it's not the only way to do it. ´GetTemplateChild´ could be used as well, and INotifyPropertyChanged can be used if you would like to avoid using a ´DependencyProperty´. - Terkel

@SimonBangTerkildsen Accessing template parts are a great alternative when you want to handle parts without adding a new dependency property, although it makes templates hard to change. - Asti

Yeah, you're completely right. on a side note, if the OP decides to use GetTemplateChild he should also add the TemplatePartAttribute to the class - Terkel

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.