首先,我是WPF和MVVM的新手,所以你可以期待一些疯狂的混音,只是建议改变,我在这里学习。
这个问题是this one的延续。
我有一个TabControl
TabItem
在XAML
中定义,每个UserControl
都有自己的<Window x:Class="WPF.GUI.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:WPF.GUI.ViewModels"
xmlns:views="clr-namespace:WPF.GUI.Views"
xmlns:mainWindow="clr-namespace:WPF.GUI.ViewModels.MainWindow"
xmlns:model="clr-namespace:WPF.Processing.Model"
xmlns:helpers="clr-namespace:WPF.GUI.Helpers"
mc:Ignorable="d">
<!--#region Resources-->
<Window.Resources>
<DataTemplate DataType="{x:Type viewModels:ResultViewModel}">
<views:ResultView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:ConfigurationViewModel}">
<views:ConfigurationView />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:DetailedViewModel}">
<views:DetailedView />
</DataTemplate>
</Window.Resources>
<!--#endregion Resources-->
<!--#region DataContext-->
<Window.DataContext>
<mainWindow:MainWindowViewModel />
</Window.DataContext>
<!--#endregion DataContext-->
<!--#region Main Grid-->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--#region Tabs-->
<TabControl>
<TabControl.Resources>
<CollectionViewSource Source="{Binding Model.TabItemsVM}" x:Key="Tabs"/>
<DataTemplate DataType="{x:Type helpers:VMWrap}">
<ContentPresenter Content="{Binding VModel}"/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemsSource>
<CompositeCollection>
<!--Result Tab-->
<TabItem Header="Result">
<ContentPresenter Content="{Binding ResultViewModel}"/>
</TabItem>
<!--Configuration Tab-->
<TabItem Header="Configuration">
<ContentPresenter Content="{Binding ConfigViewModel}"/>
</TabItem>
<!--Others-->
<CollectionContainer Collection="{Binding Source={StaticResource Tabs}}"/>
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
<!--#endregion-->
</Grid>
<!--#endregion Main Grid-->
</Window>
。
XAML
UserControl
如果我使用上面链接的答案的示例工作正常,我可以单独创建选项卡,结果选项卡和配置选项卡保持不变。
但是,如果我使用TabItem
我创建的新UserControl
我的TabItem.Header
将ObservableCollection<TabItem>
放入TabControl.DataTemplate
如果我不遵循MVVM并且我按预期创建了 select *
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in (
964855234 --int
, 141329655 --int
, 138150364 --int
, '3F3857' --varchar
)
,那么我认为问题出在select *
from dbo.BAS_GDW_TMO_PARCEL_EBILL_ALL_ARCHIVE_AUDIT_RESULT_TBL
where TMO_TRACK_NBR in (
'964855234' --varchar
, '141329655' --varchar
, '138150364' --varchar
, '3F3857' --varchar
)
任何想法或建议都将受到高度赞赏。
编辑:由于我无法提供工作示例,因为应用程序的复杂性和涉及的代码,我添加以下图片来解释问题:
答案 0 :(得分:1)
使用ContentTemplateSelector
TabControl
修复了问题
<Window.Resources>
<DataTemplate x:Key="DetailedTemplate" DataType="{x:Type viewModels:DetailedViewModel}">
<views:DetailedView/>
</DataTemplate>
<DataTemplate x:Key="ConfigurationTemplate" DataType="{x:Type viewModels:ConfigurationViewModel}">
<views:ConfigurationView />
</DataTemplate>
<DataTemplate x:Key="ResultTemplate" DataType="{x:Type viewModels:ResultViewModel}">
<views:ResultView/>
</DataTemplate>
<helpers:MyViewSelector x:Key="Selector"
DetailedTemplate="{StaticResource DetailedTemplate}"
ConfigurationTemplate="{StaticResource ConfigurationTemplate}"
ResultTemplate="{StaticResource ResultTemplate}"/>
</Window.Resources>
Tab Control
:
<TabControl Grid.Row="1"
Grid.Column="0"
VerticalAlignment="Stretch"
BorderThickness="0.5"
BorderBrush="Black"
ContentTemplateSelector="{StaticResource Selector}">
<TabControl.Resources>
<CollectionViewSource Source="{Binding Model.DetailsVM}" x:Key="Tabs"/>
<DataTemplate DataType="{x:Type viewModels:DetailedViewModel}">
<TextBlock Text="{Binding Model.SelectedItem.FileInfo.Name, Mode=OneWay}"/>
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemsSource>
<CompositeCollection>
<!--Result View-->
<TabItem Header="Import" Content="{Binding ResultViewModel}"/>
<!--Configuration Tab-->
<TabItem Header="Configuration" Content="{Binding ConfigViewModel}"/>
<!--Others-->
<CollectionContainer Collection="{Binding Source={StaticResource Tabs}}"/>
</CompositeCollection>
</TabControl.ItemsSource>
</TabControl>
辅助
public class MyViewSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object inItem, DependencyObject container)
{
if (inItem == null)
{
return ResultTemplate;
}
if (inItem is DetailedViewModel)
{
return DetailedTemplate;
}
if (inItem is ResultViewModel)
{
return ResultTemplate;
}
if (inItem is ConfigurationViewModel)
{
return ConfigurationTemplate;
}
return ResultTemplate;
}
public DataTemplate DetailedTemplate { get; set; }
public DataTemplate ConfigurationTemplate { get; set; }
public DataTemplate ResultTemplate { get; set; }
}
来源: