所有,我创建了一个包含单个控件类型的TabControl
来保存s。的Tabitem
标记如下所示MainWindow.xaml
...
<TabControl x:Name="tabControl"
ItemsSource="{Binding Path=Workspaces}"
SelectedIndex="{Binding SelectedIndex}"
IsSynchronizedWithCurrentItem="true"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TabStripPlacement="Top"
Margin="5,0,5,0">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Path=DisplayName}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</TabControl.ItemContainerStyle>
<TabControl.ContentTemplate>
<DataTemplate>
<Views:ResourceControl DataContext="{Binding}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
...
这对我Views:ResourceControl
的 s 非常有用,但我现在想扩展TabControl
. 为此,我创建了链接到从基本“WorkspaceViewModel”继承的视图模型的控件,并且我创建了以下资源文件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:ResourceStudio.ViewModels"
xmlns:Views="clr-namespace:ResourceStudio.Views">
<DataTemplate DataType="{x:Type ViewModels:ResourceDataViewModel}">
<Views:ResourceControl />
</DataTemplate>
<DataTemplate DataType="{x:Type ViewModels:StartPageViewModel}">
<Views:StartPageControl/>
</DataTemplate>
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl x:Name="tabControl"
IsSynchronizedWithCurrentItem="true"
ItemsSource="{Binding}"
SelectedIndex="{Binding SelectedIndex}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TabStripPlacement="Top"
Margin="5,0,5,0">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Path=DisplayName}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</DataTemplate>
</ResourceDictionary>
现在在MainWindow.xaml
我有
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MainWindowResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="DescriptionHeaderStyle" TargetType="Label">
<Setter Property="FontSize" Value="22" />
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Window.Resources>
...
<ContentControl
Content="{Binding Path=Workspaces}"
ContentTemplate="{StaticResource WorkspacesTemplate}">
</ContentControl>
绑定似乎注册了我想要显示的不同视图,因为选项卡显示了相关的标题。但是,在我加载另一个表单之前,实际控件不会显示。切换/加载选项卡项时,似乎SelectedIndex
没有绑定并且视图没有更新。
如何更改WorkspaceTemplate
以解决此问题?
谢谢你的时间。
编辑。请求有关 MainViewModel 的信息。
public class MainWindowViewModel : WorkspaceViewModel
{
private readonly IDialogService dialogService;
private WorkspaceViewModel selectedWorkspace;
private ObservableCollection<WorkspaceViewModel> workspaces;
private Dictionary<string, string> resourceDictionary;
public MainWindowViewModel()
{
base.DisplayName = "SomeStringName";
resourceDictionary = new Dictionary<string, string>();
dialogService = ServiceLocator.Resolve<IDialogService>();
Contract.Requires(dialogService != null);
}
...
private void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null && e.NewItems.Count != 0)
foreach (WorkspaceViewModel workspace in e.NewItems)
workspace.RequestClose += this.OnWorkspaceRequestClose;
if (e.OldItems != null && e.OldItems.Count != 0)
foreach (WorkspaceViewModel workspace in e.OldItems)
workspace.RequestClose -= this.OnWorkspaceRequestClose;
}
private void OnWorkspaceRequestClose(object sender, EventArgs e)
{
WorkspaceViewModel workspace = sender as WorkspaceViewModel;
workspace.Dispose();
int currentIndex = Workspaces.IndexOf(workspace);
this.Workspaces.Remove(workspace);
if (this.Workspaces.Count > 0)
this.SetActiveWorkspace(Workspaces[currentIndex - 1]);
}
private void SetActiveWorkspace(WorkspaceViewModel workspace)
{
Debug.Assert(this.Workspaces.Contains(workspace));
ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.Workspaces);
if (collectionView != null)
collectionView.MoveCurrentTo(workspace);
}
public WorkspaceViewModel SelectedWorkspace
{
get { return selectedWorkspace; }
set { selectedWorkspace = value; }
}
private int selectedIndex = 0;
public int SelectedIndex
{
get { return selectedIndex; }
set
{
if (selectedIndex == value)
return;
selectedIndex = value;
OnPropertyChanged("SelectedIndex");
}
}
/// <summary>
/// Returns the collection of available workspaces to display.
/// A 'workspace' is a ViewModel that can request to be closed.
/// </summary>
public ObservableCollection<WorkspaceViewModel> Workspaces
{
get
{
if (workspaces == null)
{
workspaces = new ObservableCollection<WorkspaceViewModel>();
workspaces.CollectionChanged += this.OnWorkspacesChanged;
}
return workspaces;
}
}
...
}