How hide a label when text is empty?

voidstreamvoidstream FRMember ✭✭✭

Hello,

How i can set IsVisible=false to a label when text is empty?
Which is the best way? Triggers? Converters? Behaviors?

Best regards

Tagged:

Best Answers

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Data trigger is a good choice for that.

  • voidstreamvoidstream FRMember ✭✭✭

    @ClintStLaurent
    Thanks for reply, i have more label, can i do a reusable datatrigger xaml?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I don't understand

  • voidstreamvoidstream FRMember ✭✭✭

    @ClintStLaurent said:
    I don't understand

    I have a multitude of labels on which I have to apply the DATATRIGGER. Can I do something reusable in XAML?

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The label is hiding itself when there is no value? Or the label is hiding when some other property has no value?

  • voidstreamvoidstream FRMember ✭✭✭

    The label is hiding when the Text property of this same label is empty

  • voidstreamvoidstream FRMember ✭✭✭

    @ClintStLaurent said:
    Probably easiest to bind its IsVisible property to its own Text property, through a StringNullOrEmptyConverter that returns false if there is no text.

        public class StringNullOrEmptyBoolConverter : IValueConverter
        {
            /// <summary>Returns false if string is null or empty
            /// 
            /// </summary>
            /// <param name="value"></param>
            /// <param name="targetType"></param>
            /// <param name="parameter"></param>
            /// <param name="culture"></param>
            /// <returns></returns>
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                var s = value as string;
                return !string.IsNullOrWhiteSpace(s);
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    But I have to wonder... Why is this even needed? If the text is empty then the Label has no visible presence anyway. So what is there to hide?

    The label has height, and in a vertical stacklayout of labels, you saw blank between lines if data is missing

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    it sounds like you should be using a listview bound to a collection

    that way you will only ever have space reserved for existing items.

  • voidstreamvoidstream FRMember ✭✭✭

    @JulienRosen said:
    it sounds like you should be using a listview bound to a collection

    that way you will only ever have space reserved for existing items.

    Not in this case because i custom labels. And a label can have an icon whereas another label can't have. ListView its too complex in this case (my pov).

  • JulienRosenJulienRosen CAMember ✭✭✭✭

    @voidstream you would use a data template selector to decide if a row should use a template that is only a label, or a label and an icon

  • voidstreamvoidstream FRMember ✭✭✭

    @JulienRosen said:
    @voidstream you would use a data template selector to decide if a row should use a template that is only a label, or a label and an icon

    Yes i know TemplateSelector, but everyline is unique... I really think it's not the bestway.

  • srsivaprasadsrsivaprasad USMember ✭✭
    edited December 2017

    @AlessandroCaliaro said:
    I think you can use a IValueConverter

    public class StringToBoolenConverter : IValueConverter
    {

    #region IValueConverter implementation

    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    if (value is string) {

    if(string.IsNullOrEmpty((string)value))
    return false;
    else
    return true;
    }
    return false;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException ();
    }

    #endregion
    }

    I have implemented the code but i am getting error.
    I have added

    xmlns:Converter="clr-namespace:VehicleCare.Mobile"

    And i implemenred the code like

    Label Text="{Binding PinCode}" IsVisible="{Binding PinCode, Converter={StaticResource StringNullOrEmptyBoolConverter}}">

    My purpose is to show pincode if the prop have the value.

    Am i miss anything ?

    Thanks

  • Gigex42Gigex42 USMember ✭✭✭✭

    Well what error do you get? Does your converter even trigger? (Putting breakpoint in it)

    Did you add in your xaml the definition of your converter?

    <ContentPage.Resources>
        <ResourceDictionary>
            <Converter:StringNullOrEmptyBoolConverter x:Key="StringNullOrEmptyBoolConverter" />
        </ResourceDictionary>
    </ContentPage.Resources>
    
  • Rameshbodh_Rameshbodh_ Member ✭✭

    I have Label where I'm showing some static data these labels are in Stack m performing visibilty to true and false or vice-versa. label text is not appearing as expected when stack visibility is true. I'm facing this issue only for android devices.
    Please help.

  • voidstreamvoidstream FRMember ✭✭✭

    @Rameshbodh_ said:

    I have Label where I'm showing some static data these labels are in Stack m performing visibilty to true and false or vice-versa. label text is not appearing as expected when stack visibility is true. I'm facing this issue only for android devices.
    Please help.

    How did you map your data? With properties? Are they bindable? Did you notify the view?

Sign In or Register to comment.