2011-10-02 6 views
0

이미지 컨트롤이 포함 된 사용자 지정 컨트롤을 만들었습니다.이미지 원본을 연결된 속성에 바인딩하는 방법?

ImageSource Dependency Property에 바인딩 된 Image Control의 소스를 갖고 싶습니다.

종속성 속성과 같은 생성

:

public static class ImageSourceProperty 
{ 
    public static readonly DependencyProperty CustomImageSourceProperty; 

    public static ImageSource GetCustomImageSource(DependencyObject dependencyObject) 
    { 
     return (ImageSource)dependencyObject.GetValue(CustomImageSourceProperty); 
    } 

    public static void SetCustomImageSource(DependencyObject dependencyObject, ImageSource value) 
    { 
     dependencyObject.SetValue(CustomImageSourceProperty, value); 
    } 

    static ImageSourceProperty() 
    { 
     CustomImageSourceProperty = DependencyProperty.RegisterAttached("CustomImageSource", typeof (ImageSource), typeof (ImageSourceProperty), new PropertyMetadata(default(ImageSource))); 
    } 
} 

내가 같은 사용자 지정 컨트롤의 이미지의 소스를 결합하기 위해 노력하고있어 :

<UserControl 
(...) 
xmlns:AttachedProperties="clr-namespace:Codex.UserControls.AttachedProperties" 
x:Class="Codex.UserControls.CustomControls.ImageWithBorder" 
d:DesignWidth="640" d:DesignHeight="480"> 

<Grid x:Name="LayoutRoot"> 
    <Border BorderBrush="White" BorderThickness="3" CornerRadius="3"> 
     <Image Source="{Binding AttachedProperties:ImageSourceProperty.CustomImageSource}" Width="50" Height="50"/> 
    </Border> 
</Grid> 

I 내보기에 다음과 같이 사용자 정의 컨트롤을 배치했습니다.

<CustomControls:ImageWithBorder (...) AttachedProperties:ImageSourceProperty.CustomImageSource="(...)"/> 

나는 응용 프로그램을 실행시 출력 창에 다음과 같은 오류를 얻을 :

System.Windows.Data Error: 40 : BindingExpression path error: 'AttachedProperties:ImageSourceProperty' property not found on 'object' ''ToolbarViewModel' (HashCode=20169503)'. 

왜 수 사용자 컨트롤은 종속성 속성에 바인딩되지 않는 이유는 무엇입니까? 그것의 코드 숨김에서 종속성 속성을 찾고 그것을 찾을 수 있습니까?

+0

정규 종속성 속성 대신 연결된 속성을 사용하는 이유는 무엇입니까? –

+0

ImageControl의 원본을 Dependency Property에 바인딩해야하는 다른 사용자 지정 컨트롤에서이 속성을 사용하려고 생각했습니다. 방금 바인딩이 필요한 각 사용자 지정 컨트롤에 일반 종속성 속성을 사용해야합니까? –

+0

나는 Dependency Property로 시도했지만 모든 것이 작동한다. 첨부 된 속성에 어떻게 바인딩합니까? 다른 사용자 정의 컨트롤과 함께 Dependency Property를 다시 사용할 수 있기를 바랍니다. –

답변

0

발견. 바인딩의 상대 소스를 지정하지 않았습니다.

여기에 올바른 UserControl을 선언입니다 :

<Image Source="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type CustomControls:ImageWithBorder}},Path=(AttachedProperties:ImageSourceProperty.CustomImageSource), Mode=TwoWay}" Width="50" Height="50"/> 

문제는 종속성 개체가 유효하지 않습니다 이후 종속성 속성은 값을 반환 할 수 없습니다이었다.

관련 문제