C#WPF - 在 add/remove 上绑定更新但不在更新上绑定
C#WPF - Binding updates on add/remove but not on update
在我的 WPF 应用程序中,我有一个带有编辑、添加和删除按钮的 ListView。
按删除或添加按钮,立即在 GUI 中显示更改。
按下“更新”按钮,不会更改 GUI,只有当我切换到另一个页面(使用 IPage)并返回时,才会显示该页面 refreshed/the 更改。
我也希望在更新位置的 属性 Naam 时刷新视图。
Xaml:
<ListView ItemsSource="{Binding Locations, Mode=TwoWay}" SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Naam}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Update" Command="{Binding UpdateLocationCommand}" />
<Button Content="Remove" Command="{Binding RemoveLocationCommand}" />
<Button Content="Add" Command="{Binding AddLocationCommand}" />
虚拟机:
class ClassName : ObservableObject, IPage, INotifyPropertyChanged
{
private ObservableCollection<LocatieModel> _locations = new ObservableCollection<LocatieModel>();
public ObservableCollection<LocatieModel> Locations
{
get { return _locations; }
set
{
_locations = value;
OnPropertyChange("Locaties");
}
}
private LocatieModel selectedLocation;
public LocatieModel SelectedLocation
{
get { return selectedLocation; }
set
{
selectedLocation= value;
OnPropertyChange("SelectedLocation");
}
}
public VM()
{
addLocationCommand = new Command(AddLocation, true);
updateLocationCommand = new Command(UpdateLocation, true);
removeLocationCommand = new Command(RemoveLocation, true);
}
private void AddLocation(object obj)
{
Locations.Add(new LocatieModel() { Naam = "banana1"});
}
private void RemoveLocation(object obj)
{
Locations.Remove(SelectedLocation);
}
private void UpdateLocation(object obj)
{
Locations.Single(l => l == SelectedLocation).Naam = "apple42";
}
// Part for InotificationChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string propertyname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
而 LocatieModel 只是一个模型:
public class LocatieModel
{
public int Id { get; set; }
public string Naam { get; set; }
}
如何让gui在更新时更新列表?可以自动触发吗?没主意了。谢谢你的时间。
PS:我查看了 possible answer,但这使它成为一个不可见(不存在)元素的列表。
编辑:我昨天尝试在 LocatieModel class 中实施 INotifyPropertyChanged 但没有成功。可能是某个地方的疏忽,因为现在它起作用了。谢谢:)
要更新 Naam
,您必须在 LocatieModel class.
中实施 INotifyPropertyChanged
LocatieModel 到
public class LocatieModel : INotifyPropertyChanged
{
public int Id { get; set; }
private string naam;
public string Naam
{
get { return naam; }
set
{
naam = value;
OnPropertyChange(nameof(Naam));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
在我的 WPF 应用程序中,我有一个带有编辑、添加和删除按钮的 ListView。 按删除或添加按钮,立即在 GUI 中显示更改。
按下“更新”按钮,不会更改 GUI,只有当我切换到另一个页面(使用 IPage)并返回时,才会显示该页面 refreshed/the 更改。
我也希望在更新位置的 属性 Naam 时刷新视图。
Xaml:
<ListView ItemsSource="{Binding Locations, Mode=TwoWay}" SelectedItem="{Binding SelectedLocation, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Naam}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button Content="Update" Command="{Binding UpdateLocationCommand}" />
<Button Content="Remove" Command="{Binding RemoveLocationCommand}" />
<Button Content="Add" Command="{Binding AddLocationCommand}" />
虚拟机:
class ClassName : ObservableObject, IPage, INotifyPropertyChanged
{
private ObservableCollection<LocatieModel> _locations = new ObservableCollection<LocatieModel>();
public ObservableCollection<LocatieModel> Locations
{
get { return _locations; }
set
{
_locations = value;
OnPropertyChange("Locaties");
}
}
private LocatieModel selectedLocation;
public LocatieModel SelectedLocation
{
get { return selectedLocation; }
set
{
selectedLocation= value;
OnPropertyChange("SelectedLocation");
}
}
public VM()
{
addLocationCommand = new Command(AddLocation, true);
updateLocationCommand = new Command(UpdateLocation, true);
removeLocationCommand = new Command(RemoveLocation, true);
}
private void AddLocation(object obj)
{
Locations.Add(new LocatieModel() { Naam = "banana1"});
}
private void RemoveLocation(object obj)
{
Locations.Remove(SelectedLocation);
}
private void UpdateLocation(object obj)
{
Locations.Single(l => l == SelectedLocation).Naam = "apple42";
}
// Part for InotificationChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChange(string propertyname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
而 LocatieModel 只是一个模型:
public class LocatieModel
{
public int Id { get; set; }
public string Naam { get; set; }
}
如何让gui在更新时更新列表?可以自动触发吗?没主意了。谢谢你的时间。
PS:我查看了 possible answer,但这使它成为一个不可见(不存在)元素的列表。
编辑:我昨天尝试在 LocatieModel class 中实施 INotifyPropertyChanged 但没有成功。可能是某个地方的疏忽,因为现在它起作用了。谢谢:)
要更新 Naam
,您必须在 LocatieModel class.
INotifyPropertyChanged
LocatieModel 到
public class LocatieModel : INotifyPropertyChanged
{
public int Id { get; set; }
private string naam;
public string Naam
{
get { return naam; }
set
{
naam = value;
OnPropertyChange(nameof(Naam));
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}