【问题标题】:IsFocused in MVVM TreeViewIsFocused 在 MVVM 树视图中
【发布时间】: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 这样实现焦点?

【问题讨论】:

    标签: wpf mvvm treeview focus


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      相关资源
      最近更新 更多