문제

다음 XAML은 a 내부에서 확인합니다 Window:

<Border Width="45" Height="55" CornerRadius="10" >
    <Border.Background>
        <ImageBrush>
            <ImageBrush.ImageSource>
                <CroppedBitmap Source="profile.jpg" SourceRect="0 0 45 55"/>
            </ImageBrush.ImageSource>
        </ImageBrush>    
    </Border.Background>
</Border>

그러나 동등한 코드를 사용하면 a DataTemplate 런타임에서 다음과 같은 오류가 발생합니다.

실패한 객체 초기화 (isupportinitialize.endinit). '원천' 속성이 설정되지 않았습니다. 객체에서 오류 'System.Windows.Media.Imaging.CroppedBitMap' 마크 업 파일에서.
내부 예외 :
{ " ''소스 '속성이 설정되지 않았습니다."}

유일한 차이점은 내가 가지고 있다는 것입니다 CroppedBitmap소스 속성 데이터 바운드 :

<CroppedBitmap Source="{Binding Photo}" SourceRect="0 0 45 55"/>

무엇을 제공합니까?

업데이트: An에 따르면 Bea Stollnitz의 오래된 게시물 이것은 소스 속성의 한계입니다. CroppedBitmap, 그것을 구현하기 때문에 ISupportInitialize. (이 정보는 페이지 아래에 있습니다 - "11:29"에서 검색하고 볼 수 있습니다).
.NET 3.5 SP1의 문제입니까?

도움이 되었습니까?

해결책

XAML 파서가 cruppedBitMap을 생성하면 다음과 같습니다.

var c = new CroppedBitmap();
c.BeginInit();
c.Source = ...    OR   c.SetBinding(...
c.SourceRect = ...
c.EndInit();

EndInit() 필요합니다 Source 널이 아닌 것입니다.

당신이 말할 때 c.Source=..., 값은 항상 endinit () 전에 설정되지만 사용하는 경우 c.SetBinding(...), 그것은 즉시 바인딩을하려고하지만 그것을 감지합니다. DataContext 아직 설정되지 않았습니다. 따라서 나중에 바인딩을 방어합니다. 따라서 언제 EndInit() 호출됩니다. Source 여전히 널입니다.

이 시나리오에서 변환기가 필요한 이유를 설명합니다.

다른 팁

나는 내가 완료 할 것이라고 생각했다 다른 대답 Alluded-to Converter를 제공함으로써.

이제이 변환기를 사용하고 작동하는 것처럼 보이며 더 이상 소스 '속성이 설정되지 않습니다.

public class CroppedBitmapConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        FormatConvertedBitmap fcb = new FormatConvertedBitmap();
        fcb.BeginInit();
        fcb.Source = new BitmapImage(new Uri((string)value));
        fcb.EndInit();
        return fcb;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top