xaml - Xamarin Forms 中与 ObservableCollection<string> 的两种方式绑定(bind)

标签 xaml xamarin mvvm xamarin.forms

我正在使用可绑定(bind)的StackLayout显示一系列Entry绑定(bind)到ObservableCollection<string> ( Addresses 在 vi​​ewModel 下)。

在 UI 上显示集合的内容不是问题,但是如果我修改 Entry 中任何一个的内容,它不会反射(reflect)在原始 ObservableCollection

这是 View 模型:

public class MainViewModel
    {
        public ObservableCollection<string> Addresses { get; set; }
        public ICommand AddCommand { get; private set; }

        public MainViewModel()
        {
            AddCommand = new Command(AddEmail);
            Addresses = new ObservableCollection<string>();
            Addresses.Add("test1");
            Addresses.Add("test2");
        }

        void Add()
        {
            AddCommand(string.Empty);
        }
    }

这是 View :

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="TestList.MainPage"
             x:Name="page">
    <StackLayout>

        <StackLayout Orientation="Horizontal">
            <Label Text="Addresses"
                       FontSize="Large"
                       HorizontalOptions="FillAndExpand"
                       VerticalOptions="Center"/>
            <Button Command="{Binding AddCommand}"
                    Text="+" FontSize="Title"
                        VerticalOptions="Center"/>
        </StackLayout>

        <StackLayout BindableLayout.ItemsSource="{Binding Addresses}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal">
                        <Entry Text="{Binding ., Mode=TwoWay}" HorizontalOptions="FillAndExpand"/>
                        <Button Text="-" FontSize="Title""/>
                    </StackLayout>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </StackLayout>
</ContentPage>

我怀疑这是因为我正在处理字符串,因此无法就地修改它们。您对如何在不引入包装类或类似类的情况下解决此问题有什么建议吗?

最佳答案

如果你想通过编辑 Entry 中的文本来更改代码后面的 source 的值,则需要在 ObservableCollection 类中实现接口(interface) INotifyPropertyChanged

定义模型类

public  class MyModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;



        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        string content;

        public string Content
        {

            get
            {
                return content;
            }

            set
            {
                if (content != value)
                {
                    content = value;
                    OnPropertyChanged("Content");
                }
            }
        }

    }

在 View 模型中

public ObservableCollection<MyModel> Addresses { get; set; }
Addresses = new ObservableCollection<MyModel>();
Addresses.Add(new MyModel() {Content = "test1" });
Addresses.Add(new MyModel() { Content = "test2" });

在 xaml 中

<Entry Text="{Binding Content, Mode=TwoWay}"  HorizontalOptions="FillAndExpand"/>

关于xaml - Xamarin Forms 中与 ObservableCollection<string> 的两种方式绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63192111/

相关文章:

C# WPF Datagrid 如何禁用特定列上的选择单元格

c# - wpf 命令控制可见性

c# - DateTime 与 NSDate - 哪个更适合日历实现?

android - 在没有 Mac 的情况下使用 Xamarin 时会出现什么情况?

c# - 使用 MVVM Light Messanger 代替事件

wpf - 在带有默认按钮的对话框上按 Enter 时,数据绑定(bind)不会更新属性

android - 如何在 XML 中的 TabLayout 上设置 onTabChanged 以调用 viewModel 方法?

c# - WPF:将具有不同类型项目的多个级别添加到 TreeView

c# - WPF 按钮 ControlTemplate 不会覆盖继承的前景

android - Xamarin.forms - 带有导航到主详细信息页面的登录页面有一个奇怪的布局