2011-12-29 63 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()方法。