我会在我的视图模型中这样做。0级项目的视图模型将具有:
public bool IsSelected
{
get { return false; }
set
{
// error checking is omitted
Children[0].IsSelected = value;
// let WPF know that IsSelected may have changed from what it's expecting
this.Dispatcher.BeginInvoke((ThreadStart)delegate
{
this.OnPropertyChanged(() => this.IsSelected);
});
}
}
您的XAML如下所示:
<TreeView>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
现在,每当用户单击一级项目时,虚拟机将拒绝被选择,而是选择其第一个子项目。
您可以使用完全相同的技术来处理折叠级别周围的需求。