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

前端 未结 2 1922
梦毁少年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:28

    For myself, I created the following example:

    
    
    
        
        
    
        
        pack://application:,,,/NotFound.png
    
        
        pack://application:,,,/NullImage.png
    
    
    
    
        
    
        
    
    

    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;
        }
    }
    

提交回复
热议问题