如何将绑定中的FallbackValue设置为外部图像文件的路径?
我正在尝试设置FallbackValue以防万一我的转换器无法调用,但我不知道该怎么做。
转换器中外部图像的路径看起来像那样,当LatestPosition!= null时,图像以适当的方式设置。
private static readonly ImageSource Dev1 = new BitmapImage(new Uri("/Pictures/dev1.png", UriKind.Relative)); private static readonly ImageSource Dev2 = new BitmapImage(new Uri("/Pictures/dev2.png", UriKind.Relative));
对于Image控件,当您使用URI字符串绑定Source属性时,它会自动将URI转换为BitmapImage。 但是如果将FallbackValue和TargetNullValue设置为URI字符串,则不会显示。
您需要将其设置为BitmapImage:
当我们将FallbackValue和TargetNullValue设置为BitmapImage的StaticResource时,它可以工作。
对于我自己,我创建了以下示例:
pack://application:,,,/NotFound.png pack://application:,,,/NullImage.png
我在资源中宣布了图像的路径。 图像存储在项目的根目录中。 MyTestData
列表:
public class TestDataForImage : DependencyObject { public string NotFoundString { get { return (string)GetValue(NotFoundStringProperty); } set { SetValue(NotFoundStringProperty, value); } } public static readonly DependencyProperty NotFoundStringProperty = DependencyProperty.Register("NotFoundString", typeof(string), typeof(TestDataForImage), new PropertyMetadata("")); public string NullString { get { return (string)GetValue(NullStringProperty); } set { SetValue(NullStringProperty, value); } } public static readonly DependencyProperty NullStringProperty = DependencyProperty.Register("NullString", typeof(string), typeof(TestDataForImage), new PropertyMetadata("")); public TestDataForImage() { NotFoundString = "pack://application:,,,/NotExistingImage.png"; NullString = null; } }