2012-03-10 4 views
0

私はWPFを初めて使用しています。コードウィンドウのカスタムボタンの異なるコントロールにアクセスする方法

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イメージのソースを変更したいということであるようであるの背後にある

<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> 

とコードを次のように私は、XAMLでのカスタムボタンを作成します。しかし、それは私がButtonMainMenuCatが、どのように私はXAML

答えて

2

ますその画像のソースをボタンのプロパティにバインドすることができます。これは、私が使用するアプローチです。

編集: あなたがGetTemplateChildを使用している場合、あなたはまた、あなたのクラスにTemplatePartAttributeを追加する必要があり、これは必要ですが、ちょうど良い練習されていません。

+0

私は次のステートメントを使用しました。Image menuNormal =(Image)GetTemplateChild( "MenuNormal"); nullを返します。次のステートメントで画像のソースを設定するとエラーが発生します – Rupesh

+0

@Rupesh次に、テンプレートにコントロールがない名前を使用しているか、テンプレートが適用される前に 'GetTemplateChild'を呼び出しています。 – Terkel

1

で作成されたコード内のコントロールはあなたが上Dependency Propertyを登録する必要がアクセスできることを提案してくださいMenuNormal 『

「の定義が含まれていません』というエラーがエラー与えますボタンから継承したコントロールをクリックし、イメージソースをそのプロパティにバインドします。

次に、アプリケーションコード内のそのプロパティを設定してイメージを変更できます。ここで

は導入です:あなたは別の方法として FrameworkElement.GetTemplateChild

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

を使用する必要があります後ろにコード内で'Image'のインスタンスを取得するために

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

+0

これも私がそれを行う方法ですが、それを行う唯一の方法ではありません。 'GetTemplateChild'も使用することができ、'DependencyProperty 'の使用を避けたい場合はINotifyPropertyChangedを使用できます。 – Terkel

+0

@SimonBangTerkildsenテンプレートパーツへのアクセスは、新しい依存プロパティを追加せずにパーツを処理したい場合に便利ですが、テンプレートを変更するのは難しくなります。 – Asti

+0

ええ、あなたは完全に正しいです。副作用で、OPが 'GetTemplateChild'を使用することを決定した場合、[TemplatePartAttribute](http://msdn.microsoft.com/en-us/library/system.windows.templatepartattribute.aspx)もクラスに追加する必要があります – Terkel

関連する問題