locked
Data binding controls in C# code RRS feed

  • Question

  • User388404 posted

    i have been searching for data binding but instead of being in xaml i am searching for being in .cs file, is there a manner (property) that allows me to bind the specific property to a variable?

    Monday, October 7, 2019 11:20 PM

Answers

  • User129559 posted

    SetBinding is just like XAML.

    The first property is what you are binding (ie: TextProperty of a Label).

    The second one is the name of the property on your BindingContext you are Binding to.

    EX: Using the above code.

    nameLabel.SetBinding(Label.TextProperty, "name");

    This is binding the nameLabel's Text property, to the name property on the model_class.

    Personally, I like to do

    nameLabel.SetBinding(Label.TextProperty, nameof(model_class.name));

    as I find it clearer and helps if I rename the property.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 8, 2019 9:45 PM
  • User53115 posted

    You can't bind to a static class. You must set the BindingContext somewhere in your view hierarchy to an instance of an object, and that is where the values are pulled from.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 8, 2019 10:00 PM

All replies

  • User382871 posted

    i am searching for being in .cs file, is there a manner (property) that allows me to bind the specific property to a variable Try to use SetBinding method to achieve this. ``` public partial class Page1 : ContentPage { public Page1() { InitializeComponent();

        ListView listView = new ListView();
        listView.SeparatorVisibility = SeparatorVisibility.None;
    
        ObservableCollection<model_class> models = new ObservableCollection<model_class>();
        models.Add(new model_class { });
    
        listView.ItemsSource = models;
        listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
    }
    

    }

    public class CustomViewCell : ViewCell { public CustomViewCell() { StackLayout layout = new StackLayout() { Padding = new Thickness(2, 15) }; layout.Orientation = StackOrientation.Horizontal; Label nameLabel = new Label() { HorizontalOptions = LayoutOptions.CenterAndExpand }; nameLabel.SetBinding(Label.TextProperty, "name"); nameLabel.SetBinding(Label.TextColorProperty, "color"); layout.Children.Add(nameLabel); View = layout; } }

    public class model_class : INotifyPropertyChanged { private string _name; private Color _color;

    public string name { get { return _name; } set { OnPropertyChanged(); _name = value; } }
    public Color color { get { return _color; } set { OnPropertyChanged(); _color = value; } }
    
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    } ``` Refer to: https://github.com/xamarin/xamarin-forms-samples/blob/master/UserInterface/ListView/SwitchEntryTwoBinding/twoWayBinding/twoWayBinding/View/HomeCode.cs

    Tuesday, October 8, 2019 1:43 AM
  • User388404 posted

    @Jarvan How does SetBinding works?, i have read docs about it but i just dont understand why a string as second param and why the first param, can you explain me please?

    Tuesday, October 8, 2019 9:38 PM
  • User129559 posted

    SetBinding is just like XAML.

    The first property is what you are binding (ie: TextProperty of a Label).

    The second one is the name of the property on your BindingContext you are Binding to.

    EX: Using the above code.

    nameLabel.SetBinding(Label.TextProperty, "name");

    This is binding the nameLabel's Text property, to the name property on the model_class.

    Personally, I like to do

    nameLabel.SetBinding(Label.TextProperty, nameof(model_class.name));

    as I find it clearer and helps if I rename the property.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 8, 2019 9:45 PM
  • User53115 posted

    First is the property on the view you are setting, then the name of the property on the model you are binding to.

    This C# code Label nameLabel = new Label() { HorizontalOptions = LayoutOptions.CenterAndExpand }; nameLabel.SetBinding(Label.TextProperty, "name"); nameLabel.SetBinding(Label.TextColorProperty, "color"); is equivalent to this XAML <Label HorizontalOptions="CenterAndExpand" Text="{Binding name}" TextColor="{Binding color}" />

    Tuesday, October 8, 2019 9:50 PM
  • User388404 posted

    @JoeManke so if an example i create a propery in other class, an static property, and i want this property be binded to an entry text property as following:

    entry.SetBinding(Entry.TextProperty, "StaticClass.TextEntry");

    is it ok? beacuse i have tried and it doesnt work.

    Tuesday, October 8, 2019 9:51 PM
  • User129559 posted

    Sorry, missed a word in the Binding.

    nameLabel.SetBinding(Label.TextProperty, nameof(model_class.name));

    This points to the name property appropriately. Sorry about the typo.

    Tuesday, October 8, 2019 9:56 PM
  • User53115 posted

    You can't bind to a static class. You must set the BindingContext somewhere in your view hierarchy to an instance of an object, and that is where the values are pulled from.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, October 8, 2019 10:00 PM
  • User388773 posted

    @JoeManke said: You can't bind to a static class. You must set the BindingContext somewhere in your view hierarchy to an instance of an object, and that is where the values are pulled from.

    How to bind the following in C# <Label Text="{Binding .}" />

    Friday, January 24, 2020 5:05 PM
  • User53115 posted

    @Richy

    label.SetBinding(Label.TextProperty, ".");

    Friday, January 24, 2020 8:02 PM
  • User246263 posted

    Hi @AdamMeaney , I've dynamic buttons generation in the code behind. Whenever user clicks on button I want to highlight the selected button and other buttons will be not in focus. I'm looking for Button's IsFocusedProperty to set it in code behind. I was trying below code. Any help is appreciated.

    ``` Button readingButton = new Button(); readingButton.Text = "Reading" + " " + readingCount; readingButton.HorizontalOptions = LayoutOptions.EndAndExpand; readingButton.VerticalOptions = LayoutOptions.FillAndExpand; readingButton.BorderColor = Color.FromHex("AED6F1"); readingButton.TextColor = Color.FromHex("007AC3"); readingButton.CornerRadius = 10; readingButton.BorderWidth = 1; readingButton.Padding = 10;
    readingButton.BackgroundColor = Color.Transparent;

    readingButton.SetBinding(Button.IsFocusedProperty, new Binding() { Source = readingButton, Path = "" }); ```

    Tuesday, August 11, 2020 11:45 AM
  • User129559 posted

    I would say you want something more like a Radio Button for that.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/radiobutton

    It allows only one to be selected at a time, and you can change some of the styling of it to get what you want visually out of it.

    In the coming days, they will even theoretically be a templated control you can more easily modify the entire appearance of, like so.

    Wednesday, August 12, 2020 1:36 PM