c# - 单击更改 ViewCell 元素

标签 c# android ios xamarin xamarin.forms

我建立了一个List<>项目对象的数量,这些对象被用作 ItemsSourceListView .在我的 ListView 上 ItemSelected事件,我试图更改该特定项目的元素之一。原始值与对象绑定(bind)。

请考虑以下示例:

itemClass.cs

class itemClass
{
    public itemClass()
    {
    }
    public string valueIWantToChange {get; set;}
}

MyPage.cs

public MyPage()
{
    InitializeComponent();
    List<itemClass> listOfItems= new List<itemClass>();
    itemClass item1= new itemClass { valueIWantToChange = "UnClicked"};
    listOfItems.Add(item1);
    BindingContext = this;
    lstView.ItemsSource = listOfItems;

    lstView.ItemSelected += (sender, e) =>
    {
        if (e.SelectedItem == null)
        {
            return;
        }

       // The below does nothing but highlights what I am trying to achieve
       ((itemClass)((ListView)sender).SelectedItem).valueIWantToChange = "Clicked" ;

       ((ListView)sender).SelectedItem = null;
   };
}

我的 XAML

<?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:myProject="clr-namespace:myProject"
         x:Class="myProject.MyPage">

    <StackLayout>
        <ListView x:Name="lstView">
          <ListView.ItemTemplate>
            <DataTemplate>
              <ViewCell>
                <StackLayout Padding="0" Orientation="Vertical" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding valueIWantToChange }" TextColor="Black" FontSize="16" VerticalOptions="Center" HorizontalOptions="Center" Margin="5,5,5,5"/>
                </StackLayout>
              </ViewCell>
            </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

</ContentPage>

我尝试使用绑定(bind)命令和 PropertyChangedEventHandler但没有成功。对此的任何帮助将不胜感激。

编辑

我尝试在应用程序的其他地方以类似的方式使用 PropertyChangedEventHandlers,请参见下文:

    public double FontXLarge
    {
        set
        {
            someFontsize = value;
            OnPropertyChanged("FontXLarge");
        }
        get
        {
            someFontsize = scalableFont(10);
            return someFontsize;
        }
    }        

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

上面允许我将字体大小绑定(bind)到该缩放字体,但是如果我对字符串使用类似的东西,它不会影响 ListView 中的所有项目吗?

最佳答案

我认为实现您想要的最好方法是使用带有 INotifyPropertyChangedViewModel。如果你在谷歌上搜索一下,你可以找到很多例子。通用基类的一个很好的链接是 this .

如果您找到所需的一切,请告诉我。

关于c# - 单击更改 ViewCell 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118851/

相关文章:

c# - 用户控件内联代码中的调用函数并不总是有效

c# - 在事件接收器中获取 SPContext

android - 如何将 ArrayList 保存在 Bundle 对象中

java - 如何将 getLatitude() 的值放入 textview

ios - 将 UITableView 分成固定数量的单元格

ios - 使用 Swift 在 iOS 中将 Date() 对象设置为当前时间

ios - 使用 Storyboard、自动布局和 Swift 的多行 UILabel 和 UITextView

c# - .Net POCO 的线程安全吗?

android - 卸载并重新安装android应用程序后如何保留数据

c# - 为什么 ReSharper 会反转 C# 代码的 IF?它是否提供更好的性能(甚至稍微)?