【发布时间】:2011-12-29 22:17:41
【问题描述】:
我在我的代码中实现了一个 MVVM TreeView。 我希望重点放在树中最新的 TreeViewItem 上(我继续更新树视图) 我尝试了以下方法:
<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>
在 ViewModel 中:
// 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");
}
}
}
但我收到以下错误:
错误 1 无法设置属性设置器“IsFocused”,因为它确实 没有可访问的集合访问器。第 115 行位置 29。C:\My 视觉工作室 项目\MainTreeView\View\NotificationListView.xaml 115 29
为什么我不能像 IsSelected 和 IsExpanded 这样实现焦点?
【问题讨论】: