[英]Why doesn't my view update when adding items to the ObservableCollection
So I have this project where I have two buttons and a ListView.所以我有这个项目,我有两个按钮和一个 ListView。 The ListView is separated into it's own
UserControl
with it's own ViewModel
which contains a ObservableCollection
. ListView 被分成它自己的
UserControl
和它自己的ViewModel
,其中包含一个ObservableCollection
。
I'm using a ContentPresenter
to display that control because I will be using different views.我正在使用
ContentPresenter
来显示该控件,因为我将使用不同的视图。
Currently, when I'm clicking the Log
button, it does in fact add the string to the collection, but the view doesn't update.目前,当我单击“
Log
”按钮时,它实际上确实将字符串添加到集合中,但视图不会更新。 And it keeps adding more and more everytime I click on it.每次我点击它时,它都会越来越多。 (I've put a breakpoint inside
private void AddItemOne()
to inspect it to prove that it adds items.) (我在
private void AddItemOne()
中放置了一个断点来检查它以证明它添加了项目。)
Question Why doesn't my view update when I click the "Log" button even though it's adding items.问题为什么当我点击“日志”按钮时我的视图没有更新,即使它正在添加项目。 It does add the first item if I hardcode it like this.
如果我像这样对其进行硬编码,它确实会添加第一项。
public LogViewModel()
{
Logs = new ObservableCollection<string>();
Logs.Add("Test");
}
MainWindow.xaml主窗口.xaml
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Height="25" Content="Log"
Command="{Binding AddItemOneCommand}"/>
<Button Grid.Row="0" Grid.Column="1" Height="25" Content="Other"
Command="{Binding AddItemTwoCommand}"/>
<UserControl Content="{Binding CurrentView}" Grid.Row="1" Grid.ColumnSpan="2"/>
</Grid>
MainViewModel.cs主视图模型.cs
class MainViewModel
{
public RelayCommand AddItemOneCommand { get; set; }
public RelayCommand AddItemTwoCommand { get; set; }
private object _currentView;
public object CurrentView
{
get { return _currentView; }
set
{
_currentView = value;
}
}
/* ViewModels */
public LogViewModel LogViewModel { get; set; }
public MainViewModel()
{
AddItemOneCommand = new RelayCommand(o => AddItemOne());
AddItemTwoCommand = new RelayCommand(o => AddItemTwo());
LogViewModel = new LogViewModel();
_currentView = LogViewModel;
}
private void AddItemOne()
{
LogViewModel.Logs.Add("Test");
}
private void AddItemTwo()
{
LogViewModel.Logs.Add("Test");
}
}
LogView.xaml LogView.xaml
<UserControl.DataContext>
<local:LogViewModel/>
</UserControl.DataContext>
<Grid>
<ListView ItemsSource="{Binding Logs}" Background="Gray"/>
</Grid>
LogViewModel.cs日志视图模型.cs
class LogViewModel
{
public ObservableCollection<string> Logs { get; set; }
public LogViewModel()
{
Logs = new ObservableCollection<string>();
}
}
Misc杂项
App.xaml App.xaml
<Application.Resources>
<DataTemplate DataType="{x:Type local:LogViewModel}">
<local:LogView/>
</DataTemplate>
</Application.Resources>
And the RelayCommand和 RelayCommand
public class RelayCommand : ICommand
{
private Action<object> execute;
private Func<object, bool> canExecute;
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return this.canExecute == null || this.canExecute(parameter);
}
public void Execute(object parameter)
{
this.execute(parameter);
}
}
Remove this from LogView.xaml:从 LogView.xaml 中删除:
<UserControl.DataContext>
<local:LogViewModel/>
</UserControl.DataContext>
It creates another instance of the LogViewModel
instead of using the one that you create in the MainViewModel
.它创建
LogViewModel
的另一个实例,而不是使用您在MainViewModel
中创建的实例。
You should also replace the UserControl
in MainWindow.xaml with a ContentControl
that binds to the CurrentView
property:您还应该使用绑定到
CurrentView
属性的ContentControl
替换 MainWindow.xaml 中的UserControl
:
<ContentControl Content="{Binding CurrentView}" Grid.Row="1" Grid.ColumnSpan="2" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.