我先说我怀疑这是最好的方法。
这是我的 RibbonTab 样式通知 IsSelected 绑定到视图模型中的 IsSelected
<!-- RibbonTab -->
<Style TargetType="{x:Type ribbon:RibbonTab}">
<Setter Property="ContextualTabGroupHeader" Value="{Binding ContextualTabGroupHeader}" />
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="ItemsSource" Value="{Binding GroupDataCollection}" />
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
这是视图模型代码
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected != value)
{
_isSelected = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsSelected"));
}
}
}
private bool _isSelected;
在 TabViewModel 的构造函数中,我为内容的 ViewModel 取了一个参数
public TabData(ISelectedContentTab content)
: this(content.DisplayName)
{
_selectedContent = content;
}
private ISelectedContentTab _selectedContent;
然后我使用 ItemsControl 在我的 xaml 中显示所选内容
<ItemsControl Grid.Row="1" VerticalContentAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding ElementName=ribbon,Path=SelectedItems}"
ItemTemplate="{StaticResource ContentControlTemplate}" />
而我拥有的 ContentControlTemplate 是
<DataTemplate x:Key="ContentControlTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" VerticalAlignment="Stretch" Height="Auto" VerticalContentAlignment="Stretch" Content="{Binding SelectedContent}" />
</Grid>
</DataTemplate>
还要确保你有一个数据模板将你的内容指向一个视图
希望这会有所帮助。