有没有办法在Tab加载时初始化Tab内容?我有一个问题,当创建Tab时,它不会立即初始化它的组件。只有当我点击Tab时,才会显示内容。
<TabControl
ItemsSource="{Binding Items}">
<TabControl.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding TabName}"><TextBlock.Background><SolidColorBrush /></TextBlock.Background></TextBlock>
<Button Name="btnDelete" DockPanel.Dock="Right" Margin="5,0,0,0" Padding="0" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=Name}" BorderBrush="#00000000">
<Image Source="/WPF_AccApp;component/Images/11.gif" Height="11" Width="11"></Image>
</Button>
<DockPanel.Background>
<SolidColorBrush />
</DockPanel.Background>
</DockPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<views:TabItemView />
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
按钮点击事件
private void AddInvoice_Click(object sender, RoutedEventArgs e)
{
count++;
string s = string.Format("Tab {0}", count);
mainViewModel.Items.Add(new ItemViewModel(s));
this.DataContext = mainViewModel;
if (count == 1)
{
//this is as work around
// mainViewModel.Items.Add(new ItemViewModel(s));
// mainViewModel.Items.RemoveAt(1);
}
}
答案 0 :(得分:0)
您需要添加Selected Tab Index属性并将其绑定到TabControl