2012-03-10 22 views
0

我是WPF的新手。我XAML创建一个自定义按钮,如下如何在代码窗口中访问自定义按钮的不同控制

<Button x:Class="WPFApp.ButtonMainMenuCat" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="150" d:DesignWidth="177"> 
    <Button.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Grid> 
       <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"/> 

      </Grid> 

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

       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 

和代码背后,是如下

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

     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); 
        break; 
       case "video": 
        uriSource = new Uri(@"/WPFApp;component/Resources/Vedeo_Gray.png", UriKind.Relative); 
        break; 
       case "audio": 
        uriSource = new Uri(@"/WPFApp;component/Resources/Audio_Gray.png", UriKind.Relative); 
        break; 
       case "services": 
        uriSource = new Uri(@"/WPFApp;component/Resources/Services_Gray.png", UriKind.Relative); 
        break; 
       case "shopping": 
        uriSource = new Uri(@"/WPFApp;component/Resources/Shoppings_Gray.png", UriKind.Relative); 
        break; 
       case "channels": 
        uriSource = new Uri(@"/WPFApp;component/Resources/Channels_Gray.png", UriKind.Relative); 
        break; 
       default: 
        uriSource = new Uri(@"/WPFApp;component/Resources/Home_Gray.png", UriKind.Relative); 
        break; 
      } 
      WPFApp.ButtonMainMenuCat.MenuNormal.Source = new BitmapImage(uriSource); 

     } 
    } 
} 

我现在的问题是,我想在运行时改变MenuNormal图像的来源。但它给我的错误错误ButtonMainMenuCat”不包含定义‘MenuNormal’

所以,请建议我如何可以访问代码的控制这是在XAML

回答

2

为了获得'Image'的实例在后面的代码,你将需要使用FrameworkElement.GetTemplateChild

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

作为替代你可以将该图像的来源绑定到您的按钮的属性,这是我将使用的方法。

编辑: 如果你使用GetTemplateChild那么你也应该添加TemplatePartAttribute到你的班级,这不是必需的,但只是一个很好的做法。

+0

我用下面的语句图片menuNormal =(图片)GetTemplateChild(“MenuNormal”);但它返回null。因此在下一条语句中设置图像源时出错。错误代码 – Rupesh 2012-03-10 14:31:46

+0

@Rupesh然后,您要么使用模板中没有控件的名称,要么在模板应用之前调用'GetTemplateChild'。 – Terkel 2012-03-10 14:42:57

1

创建您需要注册一个Dependency Property您从Button继承的控件,然后将图像源绑定到该属性。

然后,您可以在应用程序代码中设置该属性来更改图像。

这里的介绍:

http://www.switchonthecode.com/tutorials/wpf-tutorial-introduction-to-dependency-properties

+0

这就是我如何做到这一点,但它不是唯一的方法来做到这一点。 'GetTemplateChild'也可以使用,如果你想避免使用'DependencyProperty',可以使用INotifyPropertyChanged。 – Terkel 2012-03-10 13:54:33

+0

@SimonBangTerkildsen当你想在不添加新的依赖属性的情况下处理部件时,访问模板部件是一个很好的选择,尽管它使得模板难以改变。 – Asti 2012-03-10 14:15:10

+0

是的,你完全正确。在附注中,如果OP决定使用'GetTemplateChild',他还应该将[TemplatePartAttribute](http://msdn.microsoft.com/en-us/library/system.windows.templatepartattribute.aspx)添加到类中 – Terkel 2012-03-10 14:20:06

相关问题