Condition with binding

SreeeeSreeee INMember ✭✭✭✭✭

Hi all,

I am stuck with a problem.
I have an image inside listview viewcell. The value of image is binded with mediaUrl. I want to hide the image space if the mediaurl value is null else show the image in ui. I don't know how to check the value of mediaurl in xaml. My code is:

  <Image
    WidthRequest="250"
    HeightRequest="250"
    IsVisible=""
    Source="{Binding mediaUrl"/>

Anybody please give me a solution for adding condition inside isvisible...
Thanks in advance...

Best Answer

  • SreeeeSreeee IN ✭✭✭✭✭
    Accepted Answer

    I solved it by using triggers, My code:

                           <Image
                                    WidthRequest="250"
                                    HeightRequest="250"
                                    Source="{Binding mediaUrl}" >
                                    <Image.Triggers>
                                        <DataTrigger TargetType="Image" Binding="{Binding isMediaUrlNull}" Value="True">
                                            <Setter Property="IsVisible" Value="False" />
                                        </DataTrigger>
                                    </Image.Triggers>
                                </Image>
    

    And in model add a new bool variable:

      public bool isMediaUrlNull { get { return string.IsNullOrEmpty(mediaUrl); } }  
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I Think you can use a IValueConverter
  • SreeeeSreeee INMember ✭✭✭✭✭

    @AlessandroCaliaro Thanks for your response

    The type of mediaurl is a string, I think ivalueconverter is used for the bool values. is it?

    My issue is avoiding the blank space allocated for the picture if it's value is null else show the pic in the ui.

    The value of mediaurl is null in right pic, so the allocated space is showing in the ui. I want to avoid that and if mediaurl have a value show the picture like left picture. :)

  • rakeshraghavanrakeshraghavan INMember ✭✭
    ivalueconverter is used for the bool values..No we can converted to any values.
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    with IValueConverter you can convert from one type to another type

  • SreeeeSreeee INMember ✭✭✭✭✭
    Accepted Answer

    I solved it by using triggers, My code:

                           <Image
                                    WidthRequest="250"
                                    HeightRequest="250"
                                    Source="{Binding mediaUrl}" >
                                    <Image.Triggers>
                                        <DataTrigger TargetType="Image" Binding="{Binding isMediaUrlNull}" Value="True">
                                            <Setter Property="IsVisible" Value="False" />
                                        </DataTrigger>
                                    </Image.Triggers>
                                </Image>
    

    And in model add a new bool variable:

      public bool isMediaUrlNull { get { return string.IsNullOrEmpty(mediaUrl); } }  
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭
    I think all you need to do is bind the IsVisible attribute to your isMediaUrlNull property.

    This avoids the need for a trigger.
Sign In or Register to comment.