런타임 중에 XAML의 이미지 소스를 어떻게 변경할 수 있습니까? 지금은 임베디드 리소스 URI를 가리키고 있습니다. 뷰 모델에서 정의 된 이미지 컨트롤이 있지만 아무 것도 구속되지 않습니다. 어떻게 뷰에 표시합니까?동적으로 변경되는 XAML 이미지 원본?
0
A
답변
0
예를 들어 imageconverter를 사용할 수 있습니다. 바인딩 할 속성을 설정하면 변환기에서 값을 가져온 다음 BitmapSource를 반환하여 바인딩 할 수 있습니다.
public sealed class ImageConverter : IValueConverter
{
internal static class NativeMethods
{
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeleteObject(IntPtr hObject);
}
public BitmapSource ToBitmapSource(System.Drawing.Bitmap source)
{
BitmapSource bitSrc = null;
var hBitmap = source.GetHbitmap();
try
{
bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
}
catch (Win32Exception)
{
bitSrc = null;
}
finally
{
NativeMethods.DeleteObject(hBitmap);
}
return bitSrc;
}
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// call function to get BitmapSource
using (Bitmap bitmap = new Bitmap("{image path}"))
{
return ToBitmapSource(bitmap);
}
}
}
1
<Image x:Name="UserImage" Source="{Binding MembershipUserViewModel.UserId, Converter={StaticResource _userIdToImageConverter}, UpdateSourceTrigger=Explicit}" Stretch="Fill" />
public class UserIdToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var image = String.Format("{0}/../{1}.jpg",
Application.Current.Host.Source,
value);
var bitmapImage = new BitmapImage(new Uri(image)){CreateOptions = BitmapCreateOptions.IgnoreImageCache};
return bitmapImage;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
관련 문제
- 1. XAML 이미지 소스는 콘텐츠를 기반으로 동적으로 설정됩니다.
- 2. 동적으로 변경되는 테마
- 3. Crystal Report 2008에서 동적으로 변경되는 열
- 4. 응용 프로그램에서 동적으로 변경되는 레이아웃
- 5. CSS의 채도가 동적으로 변경되는 경우
- 6. 동적으로 변경되는 아이콘 [QT/C++]
- 7. 동적으로 XAML 문자열 수정
- 8. xaml 리소스를 동적으로 표시 하시겠습니까?
- 9. 원본 해상도로 이미지 검색
- 10. Gwt 이미지 원본 크기 :
- 11. 원본 이미지 버퍼 압축
- 12. 이미지 원본 속성 - DataTemplate 및 UserControl.Resources - 다른 어셈블리의 이미지
- 13. WPF Xaml 변환기에서 이미지
- 14. XAML 요소의 이미지 변경
- 15. 설정 targetName을 동적으로 DoubleAnimationUsingKeyFrames XAML
- 16. 동적으로 .xl을 통해 xaml 파일로드
- 17. 컨텍스트 메뉴에서 동적으로 변경되는 textSize가있는 Android AlertDialog
- 18. Gridview 열의 동적으로 변경되는 헤더 텍스트
- 19. 너비가 동적으로 변경되는 베 지어 경로
- 20. 다른 워터 마크 이미지 원본 이미지
- 21. AJAX를 사용하여 원본 이미지 데이터로드하기
- 22. KineticJS - 원본 이미지 여기 예에서
- 23. MATLAB : 원본 크기의 이미지 표시
- 24. Paperclip에서 원본 이미지 크기 조정
- 25. Wpf - 상대 이미지 원본 경로
- 26. XAML 드로잉을 이미지 파일로 내보내기
- 27. xaml 아이콘 또는 이미지 편집
- 28. XAML 이벤트로 배경 이미지 렌더링
- 29. xaml 코드에 이미지 추가 하시겠습니까?
- 30. XAML - 필요한 경우에만 이미지 배율
http://stackoverflow.com/questions/2531539/wpf-databind-image-source-in-mvvm – kenny
이들 솔루션은 모든 URI를 정의 할 필요 DataTriggers를 사용 XAML에서 이미지 컨트롤을 내 ViewModel에서 뷰로 바인딩하여 뷰가 이미지 소스와 관련되지 않도록 할 수있는 방법이 있습니까? 감사. – TheWolf