wpf TreeView 带入视图与 MVVM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15586844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:16:53  来源:igfitidea点击:

TreeView BringIntoView with MVVM

c#wpfmvvmtreeview

提问by Mare Infinitus

I want the user to be able to search for Items in a TreeView. After entering a searchtext the TreeViewItem should be scrolled into view.

我希望用户能够在 TreeView 中搜索项目。输入搜索文本后,应将 TreeViewItem 滚动到视图中。

Right now I am using the MVVM Pattern for the TreeView, for the TreeViewItems and the MainView.

现在我将 MVVM 模式用于 TreeView、TreeViewItems 和 MainView。

What do I have to do to get the functionality of BringIntoView utilizing MVVM? Is there some property I can bind? (in MFC there was something like FirstVisibileItem)

我需要做什么才能使用 MVVM 获得BringIntoView 的功能?有我可以绑定的属性吗?(在 MFC 中有类似 FirstVisibileItem 的东西)

Have seen a "solution" with a behavior. Is it really necessary?

已经看到了一个行为的“解决方案”。真的有必要吗?

采纳答案by Mare Infinitus

The problem can be solved with an Behavior.

这个问题可以通过行为来解决。

This CodeProject articledescribes it very good and it works out of the box.

这篇CodeProject 文章对其进行了很好的描述,并且开箱即用。

回答by kbisang

According to the mentioned Code Project article, here is the Code example that shows how to setup the Behavior and how to integrate the Behavior in XAML.

根据提到的代码项目文章,这里是代码示例,展示了如何设置行为以及如何在 XAML 中集成行为。

Setup the behavior:

设置行为:

/// <summary>
/// Exposes attached behaviors that can be
/// applied to TreeViewItem objects.
/// </summary>
public static class TreeViewItemBehavior
{
    #region IsBroughtIntoViewWhenSelected
    public static bool GetIsBroughtIntoViewWhenSelected(TreeViewItem treeViewItem)
    {
        return (bool)treeViewItem.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(      TreeViewItem treeViewItem, bool value)
   {
       treeViewItem.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
   }

   public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
    DependencyProperty.RegisterAttached(
    "IsBroughtIntoViewWhenSelected",
    typeof(bool),
    typeof(TreeViewItemBehavior),
    new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeViewItem item = depObj as TreeViewItem;
        if (item == null)
           return;

        if (e.NewValue is bool == false)
           return;

        if ((bool)e.NewValue)
           item.Selected += OnTreeViewItemSelected;
        else
           item.Selected -= OnTreeViewItemSelected;
    }

    static void OnTreeViewItemSelected(object sender, RoutedEventArgs e)
    {
       // Only react to the Selected event raised by the TreeViewItem
       // whose IsSelected property was modified. Ignore all ancestors
       // who are merely reporting that a descendant's Selected fired.
       if (!Object.ReferenceEquals(sender, e.OriginalSource))
         return;

       TreeViewItem item = e.OriginalSource as TreeViewItem;
       if (item != null)
          item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected
}

Then integrate the TreeViewItemBehavior in XAML:

然后在 XAML 中集成 TreeViewItemBehavior:

<TreeView.ItemContainerStyle>
  <Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="local:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True"/>
  </Style>
</TreeView.ItemContainerStyle>

Have fun :-)

玩得开心 :-)