Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Is there a way to bind the text on a label to a variable(int) in the code behind?

I have been looking around for the past 2 days trying to find a way to bind the string on a label to a var but I haven't found anything yet. Is there something like that around? Or any alternative methods?

Tagged:

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited August 24

    Do you want to binding a string value to a int attribute? For example, You have a attribute called MyProperty like following code.

     public string MyProperty { get; set; }
            public MainPage()
            {
                InitializeComponent();
                MyProperty = "100";
                BindingContext = this;
            }
    

    Then you want to bind this MyProperty to two controls, One is label's text(string), other is Label's HeightRequest(int).

    If so, you can use ValueConverter to convert it like following code. For example, I convert the string value to int, then add 100 for testing.

       class StringToIntConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                int valueConvet = int.Parse((string)value);
                return valueConvet+100;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
    
                return value;
            }
        }
    

    When I use it in the xaml.

     <ContentPage.Resources>
            <ResourceDictionary>
                <local:StringToIntConverter x:Key="StringToInt"/>
            </ResourceDictionary>
        </ContentPage.Resources>
        <StackLayout>
    
            <Label Text="{Binding MyProperty}"  x:Name="myLabel" ></Label>
            <Label  BackgroundColor="Blue" HeightRequest="{Binding MyProperty,Converter={StaticResource StringToInt}}"  />
    
    
        </StackLayout>
    
  • johnstineloljohnstinelol Member ✭✭

    Something like that but where the text property on the label is binding to an int in the code behind.

    XAML:

    <Label Text="{Binding MyProperty}"/>
    

    c#:

    int MyProperty = 1;
    
    

    If this is a really bad example I apologize. I'm very new to the world of Xamarin.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @johnstinelol Your binding way is ok, you can binding it directly, Just add public int MyProperty { get; set; } like following code.

       public partial class MainPage : ContentPage
        {
            public int MyProperty { get; set; }
            public MainPage()
            {
                InitializeComponent();
                MyProperty = 1;
                this.BindingContext = this;
            }
        }
    
Sign In or Register to comment.