この質問はかなり古く、すでに十分に回答されていますが、で選択したものを変更する別の方法を示すために、この追加の回答を追加すると思いTabItem
ましたTabControl
。それぞれのビューモデルがある場合は、プロパティを含めて、それが選択されているかどうかを判断するTabItem
と便利です。このプロパティを使用して、このプロパティをプロパティIsSelected
にデータバインドすることができます。IsSelected
TabItem.IsSelected
ItemContainerStyle
<TabControl ItemsSource="{Binding MenuItems}" TabStripPlacement="Top">
<TabControl.ItemTemplate>
<DataTemplate DataType="{x:Type ControlViewModels:MenuItemViewModel}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ImageSource}" Margin="0,0,10,0" />
<TextBlock Text="{Binding HeaderText}" FontSize="16" />
</StackPanel>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type ControlViewModels:MenuItemViewModel}">
<ContentControl Content="{Binding ViewModel}" />
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
TabItem
これで、親ビューモデルから選択したものを次のように変更できます。
MenuItems[0].IsSelected = true;
このプロパティはTabItem.IsSelected
プロパティにバインドされたデータであるため、これを呼び出すことに注意してください...:
MenuItems[1].IsSelected = true;
MenuItems[0].IsSelected
...実際には、プロパティも自動的にに設定されますfalse
。したがって、使用しているビューモデルのIsSelected
プロパティがtrueに設定されている場合は、関連するビューがで選択されていることを確認できますTabControl
。