2014-05-09 2 views
1

사용자 WPF UserControl은 이미지가 포함 된 격자이고 Source라는 ImageSource 종속성 속성에 이미지를 할당합니다. 창에서 UserControl의 ImageSource 종속성 속성에 대한 바인딩 설정 문제

public partial class MyImage : UserControl 
{ 
    public ImageSource Source 
    { 
     get { return (ImageSource)GetValue(SourceProperty); } 
     set { SetValue(SourceProperty, value); } 
    }  

    #region Source DependencyProperty 
    public static readonly DependencyProperty SourceProperty; 

    private static void SourceProperty_PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e) 
    { 
     //To be called whenever the DP is changed.    
     System.Diagnostics.Debug.WriteLine("SourceProperty changed is fired");   
    } 

    private static bool SourceProperty_Validate(object Value) 
    { 
     //Custom validation block which takes in the value of DP 
     //Returns true/false based on success/failure of the validation 
     //MessageBox.Show(string.Format("DataValidation is Fired : Value {0}", Value)); 
     return true; 
    } 

    private static object SourceProperty_CoerceValue(DependencyObject dobj, object Value) 
    { 
     //called whenever dependency property value is reevaluated. The return value is the 
     //latest value set to the dependency property 
     //MessageBox.Show(string.Format("CoerceValue is fired : Value {0}", Value)); 
     return Value; 
    } 

    #endregion 


    static MyImage() 
    { 
     SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(MyImage), 
      new FrameworkPropertyMetadata(null, 
              FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Journal, 
              new PropertyChangedCallback(SourceProperty_PropertyChanged), 
              new CoerceValueCallback(SourceProperty_CoerceValue), 
              false, UpdateSourceTrigger.PropertyChanged), 
             new ValidateValueCallback(SourceProperty_Validate)); 

    } 

    public MyImage() 
    { 
     InitializeComponent(); 
    } 
} 

나는 다음과 같이 이미지를 사용하고 (A WritableBitmap에 소스 속성입니다하여 MyClient 바인딩하려고 : 해당 UserControl에서

<UserControl x:Class="ImageOnlyClient.MyImage" 
     x:Name="MyImageControl" 
     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="300" d:DesignWidth="300"> 
<Grid Name="MainGrid"> 
    <Border Name="MyImageBorder" BorderThickness="2" BorderBrush="Orange"> 
     <Image Name="MyImage" VerticalAlignment="Top" Opacity="1" 
       RenderOptions.BitmapScalingMode="NearestNeighbor" 
       Source="{Binding Path=Source, Mode=OneWay}" /> 
    </Border> 
</Grid> 
다음과 같이 내 클래스가 정의 코드 숨김 .ImageMgr.ImageSource) 정규 이미지 컨트롤에 성공적으로 바인딩 할 수 있습니다.

<local:MyImage x:Name="imgPrimaryImage" Height="768" Width="1024" Grid.Column="1" Grid.RowSpan="2" 
       Source="{Binding Path=MyClient.ImageMgr.ImageSource}" /> 

여기에 무슨 일이 일어나고 있는지에 대한 도움을 주시면 감사하겠습니다. 나는 다음과 같은 바인딩 오류 받고 있어요 :

System.Windows.Data Error: 40 : BindingExpression path error: 'Source' property not found on 'object' ''ImageOnly' (Name='')'. BindingExpression:Path=Source; DataItem='ImageOnly' (Name=''); target element is 'Image' (Name='MyImage'); target property is 'Source' (type 'ImageSource')

+0

ImageOnly는 오류가 발생하면 UserControl – daktmacfan

답변

2

당신은 부모 UserControl을의 속성에 이미지의 "소스"를 바인드하려고하고,하지만 당신은 (소스를 지정하지 않으면 내가 바인딩 소스를 의미 ... 여기서 용어는 혼란 스럽습니다) 런타임은 기본 데이터 컨텍스트에서 속성을 찾습니다. 오류 메시지에서 "ImageOnly"유형의 클래스가 사용자 정의 컨트롤에서 상속 된 데이터 컨텍스트임을 추측합니다.

당신은 아마 다음과 같이 상대 소스를 지정하려면

: 해당 UserControl 년대

<Image ... 
     Source="{Binding 
      RelativeSource={RelativeSource AncestorType=UserControl}, 
      Path=Source, 
      Mode=OneWay}" 
     /> 
+0

을 사용하는 내 테스트 창의 클래스 이름입니다. System.Windows.Data Error : 40 : BindingExpression path error : '개체' ''MyImage '(Name ='imgPrimaryImage ')'에 'SemClient'속성이 없습니다. BindingExpression : Path = MyClient.ImageMgr.ImageSource; DataItem = 'MyImage'(Name = 'imgPrimaryImage'); 대상 요소는 'MyImage'입니다 (Name = 'imgPrimaryImage'). 대상 속성이 'Source'(유형 'ImageSource') – daktmacfan

+0

나를 위해 일했습니다. – mnyarar

1

마침내 McGarnagle의 제안 일 @ 작업을 얻었으나, 그 동안 나는 DataContext를 추가했다 =이 UserControl의 DataContext를 엉망으로 만들고있는 생성자

관련 문제