C# Xamarin:ObservableCollection没有';t自动更新列表视图
我有一个绑定到ViewModel中的ObservableCollection的ListView。 在初始化过程中,ListView项目将完美显示。 但是,当我试图在运行时更新ObservableCollection项的单个值时,listview中的链接项不会自动更新。仅当我滚动listView时,它才会更新。为什么会这样 代码如下: XAMLC# Xamarin:ObservableCollection没有';t自动更新列表视图,c#,listview,xamarin.forms,C#,Listview,Xamarin.forms,我有一个绑定到ViewModel中的ObservableCollection的ListView。 在初始化过程中,ListView项目将完美显示。 但是,当我试图在运行时更新ObservableCollection项的单个值时,listview中的链接项不会自动更新。仅当我滚动listView时,它才会更新。为什么会这样 代码如下: XAML <ListView.ItemTemplate> <DataTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Style="{StaticResource frameListView}" >
<StackLayout Margin="-15">
<Label Text="{Binding Articolo.Descrizione}"
Style="{StaticResource labelDescrizioneStyle}" />
<StackLayout Orientation="Horizontal"
HorizontalOptions="End" VerticalOptions="Center">
<Button x:Name="removeButton"
VerticalOptions="Center" HorizontalOptions="Center"
Text="-"
Font="20"
WidthRequest="45" FontAttributes="Bold"
Style="{StaticResource buttonNeutroStyle}"
Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"
CommandParameter="{Binding .}" />
<StackLayout HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" >
<Label Text="{Binding QtaEdit}"
TextColor="Black"
FontSize="18" FontAttributes="Bold"
WidthRequest="40"
HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
VerticalOptions="FillAndExpand"/>
</StackLayout>
<Button x:Name="addButton"
VerticalOptions="Center" HorizontalOptions="Center"
Text="+"
WidthRequest="45" FontAttributes="Bold"
Style="{StaticResource buttonNeutroStyle}"
Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"
CommandParameter="{Binding .}" />
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
视图模型
公共静态可观察收集获取者;
公众可观察的采集采集者
{
获取{return acquistiList;}
设置
{
if(获取者!=值)
{
获得者=价值;
OnPropertyChanged();
}
}
}
私人空间聚集(RigaStoricoModel prodotto,ParametriUM ParametriUM)
{
double esistenzaUMPredef=参数um.esistenzaUMPredef.GetValueOrDefault(0);
如果(esistenzaUMPredef>0)
{
双qtaMinima=parametriUM.qtaMinima.getValuerDefault(1);
如果(prodotto.QtaEdit+qtaMinima您是否希望获得如下结果
如果是这样,您应该像Silvermind所说的那样,在RigaStoricoModel
中实现INotifyPropertyChanged
接口
下面是MyViewModel.cs
code
public class RigaStoricoModel: INotifyPropertyChanged
{
private double _qtaEdit;
public double QtaEdit
{
set
{
_qtaEdit = value;
OnPropertyChanged("QtaEdit");
}
get => _qtaEdit;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我使用aggiungingcommand
进行测试(减少或增加)
与集合本身相关的更新由Observablecollection
的INotifyCollectionChanged
事件管理。与集合内对象相关的更新应实现INotifyPropertyChanged
并正确绑定。因此,RigaStoricoModel
是否实现INotifyPropertyChanged
?向我们展示code.Thx。现在它可以工作了。我错过了实现INotifyPropertyChanged
到RigaStoricoModel
的机会。现在它可以工作了。我错过了实现INotifyPropertyChanged
到RigaStoricoModel
。非常感谢!
public class MyViewModel
{
public static ObservableCollection<RigaStoricoModel> acquistiList;
public ObservableCollection<RigaStoricoModel> AcquistiList
{
get { return acquistiList; }
set
{
if (acquistiList != value)
{
acquistiList = value;
// OnPropertyChanged();
}
}
}
public ICommand AggiungiCommand { protected set; get; }
public ICommand AggiungiCommand2 { protected set; get; }
// public int MyProperty { get; set; }
public MyViewModel()
{
AcquistiList = new ObservableCollection<RigaStoricoModel>();
AcquistiList.Add(new RigaStoricoModel() { QtaEdit=0.28 });
AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.38 });
AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.48 });
AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.58 });
AcquistiList.Add(new RigaStoricoModel() { QtaEdit = 0.68 });
AggiungiCommand2=new Command(async (key) =>
{
RigaStoricoModel model = key as RigaStoricoModel;
model.QtaEdit += 0.1;
});
AggiungiCommand = new Command(async (key) =>
{
RigaStoricoModel model= key as RigaStoricoModel;
model.QtaEdit -= 0.1;
});
}
}
}
<StackLayout>
<!-- Place new controls here -->
<ListView ItemsSource="{Binding AcquistiList}" x:Name="mieiAcquistiStack" HasUnevenRows="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame >
<StackLayout Margin="-15" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="{Binding Articolo.Descrizione}"
/>
<StackLayout Orientation="Horizontal"
HorizontalOptions="End" VerticalOptions="Center">
<Button x:Name="removeButton"
VerticalOptions="Center" HorizontalOptions="Center"
Text="-"
Font="20"
WidthRequest="45" FontAttributes="Bold"
Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand}"
CommandParameter="{Binding .}" />
<StackLayout HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand" >
<Label Text="{Binding QtaEdit}"
TextColor="Black"
FontSize="18" FontAttributes="Bold"
WidthRequest="40"
HorizontalTextAlignment="Center" VerticalTextAlignment="Center"
VerticalOptions="FillAndExpand"/>
</StackLayout>
<Button x:Name="addButton"
VerticalOptions="Center" HorizontalOptions="Center"
Text="+"
WidthRequest="45" FontAttributes="Bold"
Command="{Binding Source={x:Reference mieiAcquistiStack}, Path=BindingContext.AggiungiCommand2}"
CommandParameter="{Binding .}" />
</StackLayout>
</StackLayout>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
public MainPage()
{
InitializeComponent();
this.BindingContext = new MyViewModel();
}