How to set FallbackValue in binding as path to external image file?

前端 未结 2 1887
梦毁少年i
梦毁少年i 2021-02-20 09:42

I\'m trying to set FallbackValue in case when my converter cannot be call, but I\'m not sure how to do that.



        
相关标签:
2条回答
  • 2021-02-20 10:05

    For the Image control, when you Binding the Source property with a URI string, it will automatically convert the URI to a BitmapImage. But if you set the FallbackValue and TargetNullValue as URI string, it will not display.

    You need to set it as BitmapImage:

    <Window.Resources>
        <BitmapImage x:Key="DefaultImage" UriSource="/Resources;component/Images/Default.jpg" />
    </Window.Resources>
    
    <Image Width="128"
                   Height="128"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Top"
                   Source="{Binding Photo,FallbackValue={StaticResource DefaultImage},
                                    TargetNullValue={StaticResource DefaultImage}}" />
    

    As we set the FallbackValue and the TargetNullValue as StaticResource of BitmapImage, It works.

    0 讨论(0)
  • 2021-02-20 10:28

    For myself, I created the following example:

    <!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" -->
    
    <Window.Resources>
        <!-- Test data -->
        <local:TestDataForImage x:Key="MyTestData" />
    
        <!-- Image for FallbackValue -->
        <sys:String x:Key="ErrorImage">pack://application:,,,/NotFound.png</sys:String>
    
        <!-- Image for NULL value -->
        <sys:String x:Key="NullImage">pack://application:,,,/NullImage.png</sys:String>
    </Window.Resources>
    
    <!-- Grid using DataContext -->
    <Grid DataContext="{StaticResource MyTestData}">
        <Image Name="ImageNull" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Source="{Binding Path=NullString, TargetNullValue={StaticResource NullImage}}" />
    
        <Image Name="ImageNotFound" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Path=NotFoundString, FallbackValue={StaticResource ErrorImage}}" />
    </Grid>
    

    The path to the images, I announced in resources. Images are stored in the root of the project. Listing of 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题