locked
Setting Height of Image through Converter

    Question

  • Hi, I have DataTemplate that binds to an RSS feed that has images.

    Sometimes one RSS item has an image, sometimes it doesn't.

    Goal: To set Image.Height to 150 if the RSS item has an image otherwise set it to zero.

    How I implemented it

    In the XAML I set the Height property to this:

    Height="{Binding ImageUrl, Converter={StaticResource ImageHeightConverter}}"

    The ImageHeightConverter looks like this:

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        // value = ImageUrl
        double height = 0;
    
        if (value != null)
        {
            if (string.IsNullOrWhiteSpace(value.ToString()) == false)
                height = 150;
        }
        return height;
    }
    

    Problem

    When the app runs the image is always expanded even though I can step through and see a zero getting returned for the height.

    Any ideas?

    Thanks,

    Mark Moeykens

    Sunday, July 6, 2014 7:43 PM

Answers

  • Hi,

    I cannot reproduce your problem. I test your code in my computer. When I set the ImageUrl to null, the image height is zero and the image cannot display. When I set a value to ImageUrl, the image can display and the height is 150:

      public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                test test = new test { name = "tt",ImageUrl="Assets/image.png"};//set a value to ImageUrl
                // test test = new test { name = "tt"};set ImageUrl to null
                image.DataContext = test;
            }
          
            
            
        }
        public class ImageConverter : IValueConverter
        {
    
            #region IValueConverter Members
    
            // Define the Convert method to change a DateTime object to 
            // a month string.
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                // value = ImageUrl
                double height = 0;
    
                if (value != null)
                {
                    if (string.IsNullOrWhiteSpace(value.ToString()) == false)
                        height =150;
                }
                return height;
            }
    
            // ConvertBack is not implemented for a OneWay binding.
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
        public class test
        {
           public string name { get; set; }
            public string ImageUrl { get; set; }
        }
    

    You should upload some code sample where your issue can be reproduced to OneDrive so that we can test it.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Tuesday, July 8, 2014 7:13 AM

All replies

  • Your code should work. Provided that the ImageUrl property actually exists on the source object, your converter should be able to set the Height of the Image element to 0. If the property doesn't exist, the converter will never be invoked.

    Please upload some code sample where your issue can be reproduced to OneDrive or similar and post the link to it here for further assistance.

    Sunday, July 6, 2014 8:20 PM
  • Hi,

    I cannot reproduce your problem. I test your code in my computer. When I set the ImageUrl to null, the image height is zero and the image cannot display. When I set a value to ImageUrl, the image can display and the height is 150:

      public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                test test = new test { name = "tt",ImageUrl="Assets/image.png"};//set a value to ImageUrl
                // test test = new test { name = "tt"};set ImageUrl to null
                image.DataContext = test;
            }
          
            
            
        }
        public class ImageConverter : IValueConverter
        {
    
            #region IValueConverter Members
    
            // Define the Convert method to change a DateTime object to 
            // a month string.
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                // value = ImageUrl
                double height = 0;
    
                if (value != null)
                {
                    if (string.IsNullOrWhiteSpace(value.ToString()) == false)
                        height =150;
                }
                return height;
            }
    
            // ConvertBack is not implemented for a OneWay binding.
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
        public class test
        {
           public string name { get; set; }
            public string ImageUrl { get; set; }
        }
    

    You should upload some code sample where your issue can be reproduced to OneDrive so that we can test it.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Tuesday, July 8, 2014 7:13 AM