我发生了一些奇怪的事情:-)
我有一个 TabControl。ItemSource 绑定到一个列表。这是 TabItemBase 的定义:
public class TabItemBase : UserControl
{
#region Properties
public virtual string TabName { get; set; }
public virtual string TabHeader { get; set; }
#endregion
}
这是我声明 TabControl 的 XAML:
<TabControl ItemsSource="{Binding Views, Mode=TwoWay}" ItemContainerStyle="{StaticResource TabItemTemplate}"
SelectedItem="{Binding SelectedView}" Name="MainTabControl" />
该样式工作正常,因为从这里开始,当我在列表中添加视图时,它可以工作,并且我在 UI 的 TabControl 中看到它。我只添加它
View.Add(new HomeView());
然后我添加了一个 NavigationService 来管理导航,因为它的名称非常明确:-)。这是代码:
public class NavigationService
{
#region ctor
public NavigationService(MainViewModel vm)
{
ViewModel = vm;
}
#endregion
#region Properties
public MainViewModel ViewModel { get; set; }
#endregion
#region Methods
public void NavigateTo(string viewName)
{
TabItemBase view = ViewModel.Views.FirstOrDefault(v => v.TabName.Equals(viewName));
if (view == null)
{
switch (viewName)
{
case ViewNames.Home:
view = new HomeView();
break;
case ViewNames.Carriers:
case ViewNames.Clients:
break;
case ViewNames.Commands:
view = new CommandsView();
break;
case ViewNames.Help:
case ViewNames.Inputs:
case ViewNames.Providers:
case ViewNames.Sorts:
case ViewNames.Users:
break;
default:
MessageBox.Show(DefaultMessages.NavigationFailed);
return;
}
ViewModel.Views.Add(view);
ViewModel.OnPropertyChanged("Views");
}
if (ViewModel.SelectedView != null && ViewModel.SelectedView.DataContext != null)
{
(ViewModel.SelectedView.DataContext as ViewModelBase).OnUnLoaded();
}
ViewModel.SelectedView = view;
(ViewModel.SelectedView.DataContext as ViewModelBase).OnLoaded();
}
#endregion
}
我这样做(点击我的按钮时):
NavigationService ns = new NavigationService(this);
ns.NavigateTo(ViewNames.Commands);
这是行不通的。它在集合中添加了视图,但我在 UI 中看不到它。
有人可以帮我解决这个问题,因为我开始掉头发了吗?
干杯
托马斯