我在 ListView 中遇到绑定(bind)问题,我遇到的问题是:
我有一个对象列表,每行都有一个减量按钮、一个数量文本和一个增量按钮。
加载此列表并单击增量按钮时,文本数量会增加 1 <- 正确
当我向下滚动并随机单击增量按钮时,文本数量不会增加(Binding 对象的值增加,但 UI 不会更新),当我再次单击时,该值是递增并在屏幕上显示 2,换句话说,仅在我第二次单击时 UI 才会更新。 <- 这就是问题
在我所做的一些测试中,按照我解释问题的上述主题,当我向上滚动(有问题的行从设备屏幕中隐藏)并再次向下滚动到同一位置时,当我单击增量按钮时,会发生同样的事情,我需要单击两次,以便 UI 更新为 4。
我正在使用 Prism MVVM,下面的代码位于 ItemSource 中项目的类中。
[JsonIgnore]
private int _quantity;
[JsonIgnore]
[Ignore]
public int Quantity {
get {
return _quantity;
} set {
SetProperty(ref _quantity, value);
RaisePropertyChanged(nameof(TextQuantity));
}
}
[JsonIgnore]
[Ignore]
public string TextQuantity
{
get
{
return string.Format("Total: {0}", Quantity);
}
}
[JsonIgnore]
[Ignore]
public ICommand ClickPlus
{
get
{
return new Command(() =>
{
Quantity += 1;
});
}
}
简而言之,如果我滚动 ListView 并尝试仅与一行中的 UI 交互,则用户需要交互两次,因此行 UI 会更新。
最佳答案
根据您的描述,您希望在单击ListView行中的按钮时更新ListView数据。我做了一个示例,您可以看一下:
<StackLayout>
<ListView x:Name="listview1" ItemsSource="{Binding models}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding name}" />
<Label
x:Name="label1"
Grid.Column="1"
Text="{Binding quality}" />
<Button
Grid.Column="2"
Command="{Binding BindingContext.command, Source={x:Reference listview1}}"
CommandParameter="{Binding Id}"
Text="increment" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public partial class Page24 : ContentPage
{
public ObservableCollection<model31> models { get; set; }
public RelayCommand1 command { get; set; }
public Page24 ()
{
InitializeComponent ();
models = new ObservableCollection<model31>()
{
new model31(){Id=1, name="cherry",quality=12},
new model31(){Id=2,name="barry",quality=31},
new model31(){Id=3,name="Wendy",quality=25},
new model31(){Id=4,name="Amy",quality=32}
};
command = new RelayCommand1(obj=>method1((int)obj));
this.BindingContext = this;
}
public void method1(int Id)
{
IEnumerable<model31> list = models.Where(x => x.Id == Id);
foreach(model31 m in list)
{
m.quality = m.quality + 1;
}
}
}
RelayCommand1类,实现命令:
public class RelayCommand1 : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public RelayCommand1(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand1(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_execute(parameter);
}
}
ViewModelBase类,实现通知。
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
关于android - Xamarin Forms - ListView 绑定(bind)更新行 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705645/