【问题标题】:Xamarin binding many entrys texts for each element of arrayXamarin 为数组的每个元素绑定许多条目文本
【发布时间】:2020-11-26 11:11:28
【问题描述】:

我正在以编程方式创建条目并以编程方式尝试将每个条目文本绑定到适当的数组元素(基于索引)。但它似乎没有约束力。 试图实现一个 mvvm。有人可以帮忙解决这个问题吗?

这是一个视图部分:

        var saveMeasurmentViewModel = new SaveMeasurementViewModel();
        BindingContext = saveMeasurmentViewModel;

        var radius = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Width * 0.32);
        var entryList = new List<Entry>();
        for (int index = 0; index < 18; index++)
        {
            var entry = new Entry
            {
                Placeholder = (index + 1).ToString(),
                TabIndex = index + 1,
            };
            object actualValueForBinding = saveMeasurmentViewModel.ActualValues[index];
            entry.SetBinding(Entry.TextProperty, nameof(actualValueForBinding));
            var frame = new Frame
            {
                Content = entry,
                Margin = new Thickness(Math.Sin(20 * index * Math.PI / 180) * radius, 0, 0, Math.Cos(20 * index * Math.PI / 180) * radius),
            };
            entryList.Add(entry);
            MainGrid.Children.Add(frame, 0, 2, 0, 1);
        }

ViewModel 属性,我正在尝试绑定:

    public double[] ActualValues
    {
        get { return actualValues; }
        set
        {
            actualValues = value;
            OnPropertyChanged();
        }
    }

【问题讨论】:

    标签: xamarin mvvm binding


    【解决方案1】:

    在您的情况下,您不能将条目文本直接绑定到列表。最好使用 BindableLayout

    在xml中

    <ScrollView Orientation="Both">
            <StackLayout  BindableLayout.ItemsSource="{Binding ActualValues}" x:Name="MainGrid" HorizontalOptions="Center"
               VerticalOptions="CenterAndExpand">
                <!-- Place new controls here -->
    
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <Grid>
    
                            <Grid.RowDefinitions>
    
                                <RowDefinition Height="Auto"  />
    
                            </Grid.RowDefinitions>
                                                   <!--Margin="{Binding Margin}"  you can also set margin with data-binding   --> 
                            <Frame  Grid.Row="0"  Margin="10,10"  WidthRequest="90" HeightRequest="50" BackgroundColor="LightBlue">
    
                                <Entry TabIndex="{Binding Id}" Text="{Binding Value}" WidthRequest="80" HeightRequest="30" TextColor="Red" />
    
                            </Frame>
    
                        </Grid>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
    
            </StackLayout>
        </ScrollView>
    

    在视图模型中

    public class MyModel:INotifyPropertyChanged
    {
    
        public MyModel(int id)
        {
    
            Value = "entry" + id.ToString(); 
            Id = id;
    
            var radius = Convert.ToInt32(DeviceDisplay.MainDisplayInfo.Width * 0.32);
    
          //  Margin = new Thickness(Math.Sin(20 * id * Math.PI / 180) * radius, 0, 0, Math.Cos(20 * id * Math.PI / 180) * radius);
    
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public Thickness Margin { get; set; }
    
        public int Id { get; set; }
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        string _value;
    
        public string Value
        {
    
            get
            {
                return _value;
            }
    
            set
            {
                if (value != _value)
                {
                    _value = value;
                    OnPropertyChanged("Value");
                }
            }
        }
    
    }
    
    public class SaveMeasurementViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
      
        public ObservableCollection<MyModel> ActualValues { get;set; }
    
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public SaveMeasurementViewModel()
        {
            ActualValues = new ObservableCollection<MyModel>() { };
    
            for (int index = 0; index < 18; index++)
            {
                ActualValues.Add(new MyModel(index) );
            }
    
        }
    
    }
    

    更多详情可以查看https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/bindable-layouts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多