2011-12-29 43 views
1

我在我的代码中实现了一个MVVM TreeView。 我想的是,重点将是在我的树最新的树型视图(我不断更新树形视图) 我尝试了以下内容:IsMocused in MVVM TreeView

<TreeView ItemsSource="{Binding NotificationViewModel}" Name="MainTree"> 
    <TreeView.ItemContainerStyle> 
     <!-- This Style binds a TreeViewItem to a NotificationViewModel. --> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
      <Setter Property="IsFocused" Value="{Binding IsFocused, Mode=TwoWay}" /> 
     </Style> 
    </TreeView.ItemContainerStyle> 

,并在视图模型:

// Constructor 
public NotificationListViewModel(Notification notification) 
{ 
    _notification = notification; 

    _activityListViewModel = new ObservableCollection<ActivityListViewModel>(); 

    _isSelected = true; 

    _isFocused = true; 
} 


private bool _isFocused; 

public bool IsFocused 
{ 
    get { return _isFocused; } 
    set 
    { 
     if (value != _isFocused) 
     { 
      _isFocused = value; 
      this.OnPropertyChanged("IsFocused"); 
     } 
    } 
} 

但我得到以下错误:

Error 1 The Property Setter 'IsFocused' cannot be set because it does not have an accessible set accessor. Line 115 Position 29. C:\My Visual Studio Projects\MainTreeView\View\NotificationListView.xaml 115 29

为什么我不能实现焦点像IsSelected和IsExpanded?

回答

1

属性IsFocused是只读的,因此不能将焦点设置到控件上。请参阅UIElement.IsFocused on MSDN了解更多细节。

您可以改为在您的树视图上使用Focus()方法。