Converter
을 만들면 맞춤 설정이 가능합니다.
[ValueConversion(typeof(string), typeof(ImageSource))]
public class EmptyImageToImageSourceConverter : IValueConverter
{
/// <summary>
/// Converts an empty string value into the DefaultImagePath property value if it exists, or a DependencyProperty.UnsetValue otherwise.
/// </summary>
public string DefaultImagePath { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || targetType != typeof(ImageSource)) return DependencyProperty.UnsetValue;
string imagePath = value.ToString();
return imagePath.IsNullOrEmpty() ? DefaultImagePath.IsNullOrEmpty() ? DependencyProperty.UnsetValue : DefaultImagePath : imagePath;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
그런 다음이처럼 사용할 수 있습니다 : 기본 Image
경로 설정 할 수 있도록하기위한 속성을 추가
<Converters:EmptyImageToImageSourceConverter x:Key="EmptyImageToImageSourceConverter"
DefaultImagePath="pack://application:,,,/AppName;component/Images/DefaultImage.png" />
주의를 그 같은 string
파일 경로와이 Converter
작품 BitMapImage
또는 ImageSource
개체가 아닙니다. 또한 표시 할 기본 이미지를 제공해야합니다.