Xamarin从EventHandler中的CollectionView更改标签

问题描述 投票:1回答:2

我有问题。我创建了一个CollectionView,每行都有一个Label作为一个计数器和一个按钮。我想要的是,当我按下按钮时,标签值将增加+1,具体取决于您单击的行。我已经像这样创建了按钮事件:

private void btnPlus_Clicked(object sender, EventArgs e)
{
    int currentValue = Convert.ToInt32(txtCounter.Text);
    txtCounter.Text = (currentValue + 1).ToString();
}

这是我的xaml:

<CollectionView ItemsSource="{Binding imageList}">
    <CollectionView.ItemsLayout>
        <GridItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout HorizontalOptions="Center" 
                <Label Text="1" TextColor="Black" x:Name="txtCounter" FontAttributes="Bold" VerticalOptions="Center"/>
                <Button CornerRadius="13" WidthRequest="25" Text="+" Padding="0"
                        HeightRequest="25" VerticalOptions="Center" Clicked="btnPlus_Clicked" />
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

但是txtCounter无法识别!?

我在做什么错?

c# xamarin xamarin.forms xamarin.android xamarin.ios
2个回答
0
投票

x:名称不在您想要的上下文中。要将模板中的属性绑定到XAML.cs Back Code上的某些内容,请使用:

PropertyOnTemplate="{Binding Path=NameOfProperty, Source={x:Reference Name=NameOfFile}}

然后,每当您更新属性NameOfProperty时,UI都会相应更新

因为有了列表,所以保留一个计数器列表,以便您可以更新由列表中所选项目索引的正确计数器


0
投票

由于使用过MVVM,因此最好处理ViewModel中的逻辑。

在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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Name="MyPage"  //set name of content page
             x:Class="xxx.MainPage">
<StackLayout HorizontalOptions="Center" 
      <Label Text="{Binding Count}" TextColor="Black" x:Name="txtCounter" FontAttributes="Bold" VerticalOptions="Center"/>
      <Button CornerRadius="13" WidthRequest="25" Text="+" Padding="0" HeightRequest="25" VerticalOptions="Center" Command="{Binding AddCommand}" CommandParameter="{Binding }"/>
</StackLayout>

在模型中

public class MyImage : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string count ;
        public string Count
        {
            set
            {
                if (count != value)
                {
                    count = value;
                    NotifyPropertyChanged("Count");
                }
            }
            get { return count; }
        }
    }

  // ... other property

在ViewModel中

public VM_CounterList()
{

   imageList= new ObservableCollection<MyImage>() {

     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },
     new MyImage(){Count="0" },

   };

   AddCommand = new Command((obj)=> {

      var myimage = obj as MyImage;

      int currentValue = Convert.ToInt32(myimage.Count);
      myimage.Count = (currentValue + 1).ToString();

   });
}
© www.soinside.com 2019 - 2024. All rights reserved.