有一个MainWindow,我通过单击菜单在UserControls之间导航,它可以正常工作。
我在Prism中使用以下模式:
https://rachel53461.wordpress.com/2011/05/08/simplemvvmexample/
保存信息后,我需要更新ViewModel, 我更新它但View不更新。 我该如何解决?
public class AdminMenuViewModel : ObservableObject, IPageViewModel
{
protected readonly IEventAggregator _eventAggregator;
public AdminMenuViewModel(IEventAggregator eventAggregator)
{
PageViewModels.Add(new FoodSupplierViewModel());
PageViewModels.Add(new ProductViewModel());
PageViewModels.Add(new UnitViewModel());
PageViewModels.Add(new PriceViewModel());
this._eventAggregator = eventAggregator;
this._eventAggregator.GetEvent<UpdateProduct>().Subscribe(UpdateProduct);
// Set starting page
CurrentUserControl = PageViewModels[0];
}
public string Name
{
get
{
return "Admin";
}
}
private List<IUserContentViewModel> _pageViewModels;
public List<IUserContentViewModel> PageViewModels
{
get
{
if (_pageViewModels == null)
_pageViewModels = new List<IUserContentViewModel>();
return _pageViewModels;
}
}
private void UpdateProduct()
{
PageViewModels[1] = new ProductViewModel(); //I update the ViewModel here
}
private void ChangeViewModel(IUserContentViewModel viewModel)
{
if (!PageViewModels.Contains(viewModel))
PageViewModels.Add(viewModel);
CurrentUserControl = PageViewModels
.FirstOrDefault(vm => vm == viewModel);
}
private ICommand _changePageCommand;
public ICommand ChangePageCommand
{
get
{
if (_changePageCommand == null)
{
_changePageCommand = new RelayCommand(
p => ChangeViewModel((IUserContentViewModel)p),
p => p is IUserContentViewModel);
}
return _changePageCommand;
}
}
}
<UserControl.Resources>
<DataTemplate DataType="{x:Type avm:FoodSupplierViewModel}">
<av:FoodSupplierView/>
</DataTemplate>
<DataTemplate DataType="{x:Type avm:ProductViewModel}">
<av:ProductView/>
</DataTemplate>
<DataTemplate DataType="{x:Type avm:UnitViewModel}">
<av:UnitView/>
</DataTemplate>
<DataTemplate DataType="{x:Type avm:PriceViewModel}">
<av:PriceView/>
</DataTemplate>
</UserControl.Resources>
<Grid Margin="5" >
<Border BorderThickness="1" BorderBrush="Black" CornerRadius="10">
<Grid>
<Border Name="mask" Background="White" CornerRadius="9"/>
<StackPanel>
<Menu Grid.Row="0" Height="58">
<MenuItem Header="General" FontSize="20" Height="38" Margin="10" Foreground="SteelBlue">
<ItemsControl ItemsSource="{Binding PageViewModels}" Width="168" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding}" TextDecorations="{x:Null}">
<InlineUIContainer>
<TextBlock Text="{Binding Name}" FontSize="18"/>
</InlineUIContainer>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</MenuItem>
</Menu>
</StackPanel>
</Grid>
</Border>
<Border Grid.Row="1" BorderThickness="1" CornerRadius="8" >
<ContentControl Content="{Binding CurrentUserControl}" Margin="0,0,-1,0"/>
</Border>
</Grid>
答案 0 :(得分:4)
如果要在该集合中添加/删除时通知,请使用ObservableCollection<T>
代替List<T>
,如果项目中有更新,请使用OnPropertyChanged