我有一个问题,TabItem内容不显示数据,直到我点击另一个TabItem,然后我点击回到第一个TabItem。
我理解WPF TabItems可能会使用虚拟化,并且禁用可见的TabItems
虽然,我在谈论启动时可见的TabItem。
更多细节:
我的主窗口由TabControl组成
此TabControl ContentTemplate由用户控件组成。
该用户控件也由TabControl组成。
在主窗口和用户控件中,我设置了属性SelectedIndex =" 0"。
当我启动应用程序时,可见的TabItem不显示数据 虽然我可以看到显示数据的线程正在运行 如果我单击另一个TabItem然后返回第一个TabItem,则数据会正确显示。
所以我的问题是:我应该在应用程序启动时在可见的TabItem中显示数据?
一些代码:
MainWindow.xaml
<Window x:Class="Appname.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:local="clr-namespace:Appname.Views"
xmlns:race="clr-namespace:Appname.Controls.Race"
xmlns:emc="clr-namespace:Appname.Controls.ExpandingMenu"
xmlns:control="clr-namespace:Appname.Controls"
xmlns:wpft="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="appname" Height="800" Width="1024">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="224" />
<ColumnDefinition Width="4" />
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<!--Left Menu-->
<emc:ExpandingMenuControl DataContext="{Binding ExpandingMenu}" />
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
<!--Right part-->
<TabControl ItemsSource="{Binding Markets}" SelectedIndex="0" Grid.Column="2" Name="TabMarkets" >
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource TabStyle}" />
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MarketName}" Margin="4,0" Padding="4,0" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<race:RaceControl DataContext="{Binding RaceControlViewModel }"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
RaceControl.xaml
<UserControl x:Class="Appname.Controls.Race.RaceControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Appname.Controls"
xmlns:uc="clr-namespace:Appname.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<!--Races Tabs-->
<TabControl ItemsSource="{Binding Races}" SelectedIndex="0" Name="TabRaces">
<TabControl.Resources>
<Style TargetType="TabItem" BasedOn="{StaticResource TabStyle}" />
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=StartTime, StringFormat={}{0:HH:mm}}" Padding="4,0" />
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ListBox ItemsSource="{Binding Runners, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<uc:SelectionControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!--Market Panel-->
<uc:MarketPanelControl Grid.Column="1" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TabControl}}, Path=DataContext.MarketPanel}"/>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
MainWindowViewModel.cs
private ObservableCollection<MarketModel> _markets;
public ObservableCollection<MarketModel> Markets
{
get { return _markets; }
set
{
if (_markets != value)
{
_markets = value;
OnPropertyChanged();
}
}
}
RaceControlViewModel.cs
public ObservableCollection<RaceModel> Races { get; private set; }