2012-03-10 57 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

相關問題