我将自己的ViewModel绑定到TabControl ItemSource并使用模板自定义内容和标题行为。这一切都运行正常,但在代码中,当我执行TabControl.SelectedTab.Name时 - 此字段为空,因为这是您通常在TabItem结构中传递的内容。
如何绑定ViewModel属性以便将其设置为TabItems Name属性?
我的tabcontrol XAML
<TabControl
Margin="0,-2,0,0"
x:Name="SelectionTabs"
Style="{StaticResource DetailTabControl}"
ItemsSource="{Binding DetailTabs}"
SelectedValue="{Binding SelectedTab, Mode=TwoWay}"
ItemContainerStyle="{StaticResource DetailTabItem}" >
<TabControl.Resources>
<DataTemplate DataType="{x:Type detailTabs:ValuationTabViewModel}">
<detailTabs1:ValuationTab Margin="0,10,0,10" />
</DataTemplate>
</TabControl.Resources>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}" />
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
我的viewmodel代码
public class TabViewModel : ViewModelBase
{
private string _header;
private BaseTabContentViewModel _content;
public string Header
{
get => _header;
set
{
_header = value;
RaisePropertyChanged(nameof(Header));
}
}
public BaseTabContentViewModel Content
{
get => _content;
set
{
_content = value;
RaisePropertyChanged(nameof(Content));
}
}
public TabViewModel(string header, BaseTabContentViewModel viewModel)
{
Header = header;
Content = viewModel;
}
}