Data binding controls in C# code

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?

Best Answers

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    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

  • AndresQuiValAndresQuiVal Member ✭✭

    @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?

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited October 8

    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}" />
    
  • AndresQuiValAndresQuiVal Member ✭✭
    edited October 8

    @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.

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    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.

Sign In or Register to comment.