2016-10-29 13 views
0

添付プロパティを持つ添付プロパティおよびControlTemplateがあります。 添付Properyは:私もテンプレートWPFの添付プロパティXAML

xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses" 
<ToggleButton Style="{StaticResource togglebutton_topic_menu_normal}" m:togglebuttonimage.togglebuttonimagesource="accept.png" /> 

しかし、上記の名前空間を追加しました:

using System.Windows; 

namespace NoteProjectV2.classes.frmtopicclasses 
{ 
    class togglebuttonimage : DependencyObject 
    { 
     public static readonly DependencyProperty togglebuttonimagesource = DependencyProperty.RegisterAttached("ImageSource", typeof(string), typeof(togglebuttonimage), new PropertyMetadata(default(string))); 


     public static void Settogglebuttonimagesource(UIElement element, string value) 
     { 
      element.SetValue(togglebuttonimagesource, value); 
     } 

     public static string Gettogglebuttonimagesource(UIElement element) 
     { 
      return (string)element.GetValue(togglebuttonimagesource); 
     } 


    } 
} 

これは私のコントロールテンプレートである私は、コードでproperyを添付使用

<Application x:Class="NoteProjectV2.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:NoteProjectV2" 
      xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses" 
      StartupUri="frmTopic.xaml"> 
    <Application.Resources> 

     <Style x:Key="togglebutton_topic_menu_normal" TargetType="ToggleButton"> 
      <Setter Property="Width" Value="40" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ToggleButton"> 
         <Border Name="border"> 
          <Border.Style> 
           <Style> 
            <Setter Property="Border.Background" Value="Black"/> 

           </Style> 
          </Border.Style> 
          <Image Width="22" Height="22" Name="image" > 
           <Image.Style> 
            <Style> 
    Only This is not working===============> <Setter Property="Image.Source" Value="{Binding Path=(m:togglebuttonimage.togglebuttonimagesource),RelativeSource={RelativeSource TemplatedParent}}" /> 
            </Style> 
           </Image.Style> 
          </Image> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Application.Resources> 
</Application> 

(私はトグルボタンでこれを使用しました)これは動作していませんどこに問題がありますか?

+1

依存プロパティの命名規則に従わない。どうぞご覧ください。[ここにあります](http://stackoverflow.com/questions/7139641/how-to-use-attached-property-within-a-style) – gomi42

+0

そして、あなたは確かに 'togglebuttonimagesource'が恐ろしいですメソッドの名前はもちろん、プロパティの名前lile 'Settogglebuttonimagesource'。代わりに[Pascal Casing](https://msdn.microsoft.com/en-us/library/ms229043(v = vs.110).aspx)を使用してください。 – Clemens

答えて

1

クラスDependencyPropertyのRegisterメソッドとRegisterAttachedメソッドの最初の引数は、プロパティの名前です。

"ImageSource"という名前を使用している間は、実際には"ToggleButtonImageSource"(適切なケースを使用しています)である必要があります。また、アタッチされたプロパティのみを宣言する限り、所有するクラスはDependencyObjectから派生する必要はありません。そのほかに

public class ToggleButtonImage 
{ 
    public static readonly DependencyProperty ToggleButtonImageSourceProperty = 
     DependencyProperty.RegisterAttached(
      "ToggleButtonImageSource", typeof(string), typeof(ToggleButtonImage)); 

    public static void SetToggleButtonImageSource(UIElement element, string value) 
    { 
     element.SetValue(ToggleButtonImageSourceProperty, value); 
    } 

    public static string GetToggleButtonImageSource(UIElement element) 
    { 
     return (string)element.GetValue(ToggleButtonImageSourceProperty); 
    } 
} 

、あなたはより良いプロパティの型としてImageSourceの代わりstringを使用する必要があります。

関連する問題