2010-04-28 7 views
1

내가문제 바인딩 이미지 소스 종속성 속성

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Local:ImageButton}"> 
       <StackPanel Height="Auto" Orientation="Horizontal"> 
       <Image Margin="0,0,3,0" Source="{Binding ImageSource}" /> 
       <TextBlock Text="{TemplateBinding Content}" /> 
       </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
    </Setter> 
</Style> 

하여 ImageButton 클래스로하여 ImageButton에 대한 사용자 지정 컨트롤을 만든 내가 같은 이미지에 ImageSource 결합 할 수 아니에요 그러나

public class ImageButton : Button 
    { 
     public ImageButton() : base() { } 

     public ImageSource ImageSource 
     { 
      get { return base.GetValue(ImageSourceProperty) as ImageSource; } 
      set { base.SetValue(ImageSourceProperty, value); } 
     } 
     public static readonly DependencyProperty ImageSourceProperty = 
      DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton)); 
    } 

처럼 보인다 : (이 코드는 UI 폴더에 이미지 리소스 폴더에)

<Local:ImageButton x:Name="buttonBrowse1" Width="100" Margin="10,0,10,0" 
Content="Browse ..." ImageSource="../Resources/BrowseFolder.bmp"/> 

그러나 간단한 이미지를 찍으면 같은 소스가 지정되면 표시됩니다. 아무에게도 무엇을해야한다고 말할 수 있습니까?

답변

2

귀하는 컨텐츠 속성에했던 것과 같은 TemplateBinding하여 ControlTemplate에있는 Binding을 대체해야합니다

<Image Margin="0,0,3,0" Source="{TemplateBinding ImageSource}" /> 

또한, 당신의 DependencyProperty에 대한 정의가 올바르지 않습니다. 나는이 이름 충돌 문제를 일으키는 곳/여부를 알 수없는

DependencyProperty.Register("ImageSource", typeof(ImageSource), ... 

하지만 적어도 매우 실제 CLR 속성의 정확한 이름을 사용하는 것이 좋습니다 : 문자열 대신 SourceImageSource 읽어야합니다.

편집 : 당신은 또한 당신의 ImageButton에 스타일의 TargetType을 변경해야합니다 :

<Style TargetType="{x:Type Local:ImageButton}"> 
+0

이름 충돌 문제가 발생할 않습니다. –

+0

Binding을 TemplateBinding으로 바꾸면 컴파일 타임 오류가 발생합니다. 'TemplateBindingExtension'유형의 인스턴스를 만들 수 없습니다. – Archie

+0

내 편집 .... – gehho