[英]Xamarin.forms: How to get Object value from a Listview
I am placing a stepper inside a Listview.我在 Listview 中放置了一个步进器。 I would like to know which Item the Stepper belong to when I change it.当我改变它时,我想知道步进器属于哪个项目。 Here is the Listview.这是列表视图。 I would like to know, when I modify the stepper's value, which EAN is part of the same ViewCell.我想知道,当我修改步进器的值时,哪个 EAN 是同一个 ViewCell 的一部分。
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemTapped="OnItemSelected"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" Orientation="Vertical" HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}" LineBreakMode="WordWrap" FontSize="16" HorizontalOptions="FillAndExpand" />
<Label Text="{Binding QuantityExpectedDisplay}" LineBreakMode="WordWrap" FontSize="10" HorizontalOptions="FillAndExpand" />
<Label Text="{Binding EAN}" LineBreakMode="WordWrap" FontSize="10" HorizontalOptions="FillAndExpand" />
<Stepper Value="{Binding QuantityDefect}" Minimum="0" Maximum="{Binding Quantity}" HorizontalOptions="FillAndExpand" ValueChanged="Stepper_ValueChanged" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
So that I can use that ean later to find the corresponding line.这样我以后可以使用该 ean 来查找相应的行。 (In the example, I would like to find the ListItem, or at least its EAN in test, rather than putting a new one) (在示例中,我想在测试中找到 ListItem,或者至少是它的 EAN,而不是放一个新的)
private void Stepper_ValueChanged(object sender, ValueChangedEventArgs e)
{
ListItem test = new ListItem();//Here I'd like the ViewCell Values
var line = viewModel.lines.Find(l => l.Ean == test.EAN);
line.QuantityOpen = (int)e.NewValue;
}
Thank you and have a nice day谢谢你,祝你有美好的一天
We generally don't recommend using this method,you can implement INotifyPropertyChanged
for your the item of Items
.我们一般不推荐使用这种方法,你可以为你的Items
实现INotifyPropertyChanged
。
Suppose the item model is Item.cs
, you can implement INotifyPropertyChanged
as follows:假设项目模型为Item.cs
,您可以按如下方式实现INotifyPropertyChanged
:
public class Item: INotifyPropertyChanged
{
public string Name { get; set; }
public string QuantityExpectedDisplay{ get; set; }
public string EAN{ get; set; }
//public int QuantityDefect { get; set; }
int _quantityDefect;
public int QuantityDefect
{
set { SetProperty(ref _quantityDefect, value); }
get { return _quantityDefect; }
}
public int Quantity { get; set; }
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
And in YourPage.xaml
,we can do like this:在YourPage.xaml
中,我们可以这样做:
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10" Orientation="Vertical" HorizontalOptions="FillAndExpand">
<Label Text="{Binding Name}" LineBreakMode="WordWrap" FontSize="16" HorizontalOptions="FillAndExpand" />
<Label Text="{Binding QuantityExpectedDisplay}" LineBreakMode="WordWrap" FontSize="10" HorizontalOptions="FillAndExpand" />
<Label Text="{Binding EAN}" LineBreakMode="WordWrap" FontSize="10" HorizontalOptions="FillAndExpand" />
<Stepper Value="{Binding QuantityDefect}" Minimum="0" Maximum="10" Increment="1" HorizontalOptions="FillAndExpand" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Note:笔记:
Once we change the stepper's value, the value of QuantityDefect
will change accordingly.一旦我们改变步进器的值, QuantityDefect
的值就会相应改变。
Update更新
How can I find more information about this type of binding?如何找到有关此类绑定的更多信息? How is it called, so I can find out how it works for later?它是怎么称呼的,所以我以后可以了解它是如何工作的?
Yes, if we implement INotifyPropertyChanged
for Item and change the value, it also change the value of the ItemsSource
of current listview.是的,如果我们为 Item 实现INotifyPropertyChanged
并更改值,它也会更改当前列表视图的ItemsSource
的值。
For example,, suppose the view model of current page is MyViewModel.cs
:例如,假设当前页面的视图模型是MyViewModel.cs
:
public class MyViewModel
{
public ObservableCollection<Item> items { get; set; }
public MyViewModel() {
items = new ObservableCollection<Item>();
items.Add(new Item { Name = "Tomato", Quantity = 100, QuantityDefect = 10 });
items.Add(new Item { Name = "Romaine Lettuce", Quantity = 100, QuantityDefect = 10 });
items.Add(new Item { Name = "Zucchini", Quantity = 100, QuantityDefect = 10 });
}
}
Then we can also get the modified ItemsSource
as follows(I added a Button to print the data):然后我们也可以得到修改后的ItemsSource
如下(我加了一个Button来打印数据):
public partial class MainViewXaml : ContentPage
{
MyViewModel viewModel;
public MainViewXaml ()
{
InitializeComponent();
viewModel = new MyViewModel();
BindingContext = viewModel;
}
private void Stepper_ValueChanged(object sender, ValueChangedEventArgs e)
{
}
// we can get the items here
private void Button_Clicked(object sender, System.EventArgs e)
{
foreach (Item item in viewModel.items) {
System.Diagnostics.Debug.WriteLine(" name : " + item.Name + "<-----> QuantityDefect = " + item.QuantityDefect);
}
}
}
First, see Jessie's answer for the basics of Data Binding to a list of items.首先,请参阅 Jessie 的回答,了解将数据绑定到项目列表的基础知识。
Or see example described in ListView Data Sources .或者参见ListView Data Sources中描述的示例。
In addition, we need a way to tell viewmodel what item has changed.此外,我们需要一种方法来告诉 viewmodel 哪些项目发生了变化。
MessagingCenter can do this.消息中心可以做到这一点。
MyViewModel.cs: MyViewModel.cs:
public MyViewModel()
{
MessagingCenter.Subscribe<MyItem>(this, "changed", Item_Changed);
}
public ObservableCollection<MyItem> Items { get; set; }
private void Item_Changed(MyItem item)
{
var index = Items.IndexOf(item);
// Make sure it is mine.
if (index >= 0)
{
// ...
}
}
MyItem.cs: MyItem.cs:
int _quantityDefect;
public int QuantityDefect
{
get => _quantityDefect;
set
{
SetProperty(ref _quantityDefect, value);
Xamarin.Forms.MessagingCenter.Send<MyItem>(this, "changed");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.