如何取消用户的 WPF TreeView 点击?

14

我有一个带有树形视图控件的WPF应用程序。

当用户单击树上的节点时,页面上的其他文本框、组合框等控件将被填充相应的值。

然后,用户可以更改这些值,并通过单击保存按钮保存更改。

但是,如果用户在没有保存更改的情况下选择了不同的树形视图节点,则我希望显示一个警告和取消该选择的机会。

消息框:继续并丢弃未保存的更改? 确定/取消 http://img522.imageshack.us/img522/2897/discardsj3.gif

XAML...

<TreeView Name="TreeViewThings"
    ...
    TreeViewItem.Unselected="TreeViewThings_Unselected"
    TreeViewItem.Selected="TreeViewThings_Selected" >

如何取消这些未选择/选择事件?

Sub TreeViewThings_Unselected(ByVal sender As System.Object, _
                              ByVal e As System.Windows.RoutedEventArgs)
    Dim OldThing As Thing = DirectCast(e.OriginalSource.DataContext, Thing)
    If CancelDueToUnsavedChanges(OldThing) Then
        '放置取消代码此处
    End If
End Sub
Sub TreeViewThings_Selected(ByVal sender As System.Object, _ ByVal e As System.Windows.RoutedEventArgs) Dim NewThing As Thing = DirectCast(e.OriginalSource.DataContext, Thing) PopulateControlsFromThing(NewThing) End Sub

更新:我已经提出了一个后续问题...
如何正确处理具有MessageBox确认的PreviewMouseDown事件?


9
无关的抱怨:请不要设计一个询问是否的消息框,但提供确定/取消按钮。 - scwagner
9个回答

13

更新

我意识到可以将逻辑放在SelectedItemChanged中。这是一个更加简洁的解决方案。

Xaml

<TreeView Name="c_treeView"
          SelectedItemChanged="c_treeView_SelectedItemChanged">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

后台代码。我有一些类是TreeView的ItemsSource,因此我创建了一个接口(MyInterface),它为它们所有的类公开了IsSelected属性。

private MyInterface m_selectedTreeViewItem = null;
private void c_treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if (m_selectedTreeViewItem != null)
    {
        if (e.NewValue == m_selectedTreeViewItem)
        {
            // Will only end up here when reversing item
            // Without this line childs can't be selected
            // twice if "No" was pressed in the question..   
            c_treeView.Focus();   
        }
        else
        {
            if (MessageBox.Show("Change TreeViewItem?",
                                "Really change",
                                MessageBoxButton.YesNo,
                                MessageBoxImage.Question) != MessageBoxResult.Yes)
            {
                EventHandler eventHandler = null;
                eventHandler = new EventHandler(delegate
                {
                    c_treeView.LayoutUpdated -= eventHandler;
                    m_selectedTreeViewItem.IsSelected = true;
                });
                // Will be fired after SelectedItemChanged, to early to change back here
                c_treeView.LayoutUpdated += eventHandler;
            }   
            else
            {
                m_selectedTreeViewItem = e.NewValue as MyInterface;
            }        
        }
    }
    else
    {
        m_selectedTreeViewItem = e.NewValue as MyInterface;
    }
}

我还没有发现任何情况下按“否”不会恢复到先前的项目。

4

我曾经在我的应用程序中解决过同样的问题,但是需要在多个树形视图中解决。我派生了TreeView并添加了事件处理程序,部分使用了Meleak的解决方案,部分使用了来自这个论坛的扩展方法:http://forums.silverlight.net/t/65277.aspx/1/10

我想与你分享我的解决方案,这是我完整可重用的TreeView,可以处理“取消节点更改”:

public class MyTreeView : TreeView
{
    public static RoutedEvent PreviewSelectedItemChangedEvent;
    public static RoutedEvent SelectionCancelledEvent;

    static MyTreeView()
    {
        PreviewSelectedItemChangedEvent = EventManager.RegisterRoutedEvent("PreviewSelectedItemChanged", RoutingStrategy.Bubble,
                                                                           typeof(RoutedPropertyChangedEventHandler<object>), typeof(MyTreeView));

        SelectionCancelledEvent = EventManager.RegisterRoutedEvent("SelectionCancelled", RoutingStrategy.Bubble,
                                                                   typeof(RoutedEventHandler), typeof(MyTreeView));
    }

    public event RoutedPropertyChangedEventHandler<object> PreviewSelectedItemChanged
    {
        add { AddHandler(PreviewSelectedItemChangedEvent, value); }
        remove { RemoveHandler(PreviewSelectedItemChangedEvent, value); }
    }

    public event RoutedEventHandler SelectionCancelled
    {
        add { AddHandler(SelectionCancelledEvent, value); }
        remove { RemoveHandler(SelectionCancelledEvent, value); }
    }


    private object selectedItem = null;
    protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
    {
        if (e.NewValue == selectedItem)
        {
            this.Focus();

            var args = new RoutedEventArgs(SelectionCancelledEvent);
            RaiseEvent(args);
        }
        else
        {
            var args = new RoutedPropertyChangedEventArgs<object>(e.OldValue, e.NewValue, PreviewSelectedItemChangedEvent);
            RaiseEvent(args);

            if (args.Handled)
            {
                EventHandler eventHandler = null;
                eventHandler = delegate
                {
                    this.LayoutUpdated -= eventHandler;

                    var treeViewItem = this.ContainerFromItem(selectedItem);
                    if (treeViewItem != null)
                        treeViewItem.IsSelected = true;
                };

                this.LayoutUpdated += eventHandler;
            }
            else
            {
                selectedItem = this.SelectedItem;
                base.OnSelectedItemChanged(e);
            }
        }
    }
}

public static class TreeViewExtensions
{
    public static TreeViewItem ContainerFromItem(this TreeView treeView, object item)
    {
        if (item == null) return null;

        var containerThatMightContainItem = (TreeViewItem)treeView.ItemContainerGenerator.ContainerFromItem(item);

        return containerThatMightContainItem ?? ContainerFromItem(treeView.ItemContainerGenerator, treeView.Items, item);
    }

    private static TreeViewItem ContainerFromItem(ItemContainerGenerator parentItemContainerGenerator, ItemCollection itemCollection, object item)
    {
        foreach (var child in itemCollection)
        {
            var parentContainer = (TreeViewItem)parentItemContainerGenerator.ContainerFromItem(child);
            var containerThatMightContainItem = (TreeViewItem)parentContainer.ItemContainerGenerator.ContainerFromItem(item);

            if (containerThatMightContainItem != null)
                return containerThatMightContainItem;

            var recursionResult = ContainerFromItem(parentContainer.ItemContainerGenerator, parentContainer.Items, item);
            if (recursionResult != null)
                return recursionResult;
        }
        return null;
    }
}

以下是一个使用示例(窗口包含MyTreeView的代码后台):

    private void theTreeView_PreviewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (e.OldValue != null)
            e.Handled = true;
    }

    private void theTreeView_SelectionCancelled(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Cancelled");
    }

在树形视图中选择第一个节点后,所有其他节点的更改都将被取消,并显示一个消息框。

谢谢,这正是我需要的,以防止TreeView中节点的崩溃导致其被选中。唯一缺少的一点是如何在代码后台中连接事件处理程序: EventManager.RegisterClassHandler(typeof(MyTreeView), MyTreeView.PreviewSelectedItemChangedEvent, new RoutedPropertyChangedEventHandler<object>(theTreeView_PreviewSelectedItemChanged)); EventManager.RegisterClassHandler(typeof(MyTreeView), MyTreeView.SelectionCancelledEvent, new RoutedEventHandler(theTreeView_SelectionCancelled)); - GCymbala

4

如果逻辑已经存在于OnSelectedItemChanged方法中,实际上已经选定了新项,因此您无法将逻辑放入其中。

如另一位帖子所建议的,PreviewMouseDown处理程序是实现逻辑的更好位置,但仍需要做大量工作。

以下是我的意见:

首先,我实现的TreeView:

public class MyTreeView : TreeView
{
    static MyTreeView( )
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(MyTreeView),
            new FrameworkPropertyMetadata(typeof(TreeView)));
    }

    // Register a routed event, note this event uses RoutingStrategy.Tunnel. per msdn docs
    // all "Preview" events should use tunneling.
    // http://msdn.microsoft.com/en-us/library/system.windows.routedevent.routingstrategy.aspx
    public static RoutedEvent PreviewSelectedItemChangedEvent = EventManager.RegisterRoutedEvent(
        "PreviewSelectedItemChanged",
        RoutingStrategy.Tunnel,
        typeof(CancelEventHandler),
        typeof(MyTreeView));

    // give CLR access to routed event
    public event CancelEventHandler PreviewSelectedItemChanged
    {
        add
        {
            AddHandler(PreviewSelectedItemChangedEvent, value);
        }
        remove
        {
            RemoveHandler(PreviewSelectedItemChangedEvent, value);
        }
    }

    // override PreviewMouseDown
    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        // determine which item is going to be selected based on the current mouse position
        object itemToBeSelected = this.GetObjectAtPoint<TreeViewItem>(e.GetPosition(this));

        // selection doesn't change if the target point is null (beyond the end of the list)
        // or if the item to be selected is already selected.
        if (itemToBeSelected != null && itemToBeSelected != SelectedItem)
        {
            bool shouldCancel;

            // call our new event
            OnPreviewSelectedItemChanged(out shouldCancel);
            if (shouldCancel)
            {
                // if we are canceling the selection, mark this event has handled and don't
                // propogate the event.
                e.Handled = true;
                return;
            }
        }

        // otherwise we want to continue normally
        base.OnPreviewMouseDown(e);
    }

    protected virtual void OnPreviewSelectedItemChanged(out bool shouldCancel)
    {
        CancelEventArgs e = new CancelEventArgs( );
        if (PreviewSelectedItemChangedEvent != null)
        {
            // Raise our event with our custom CancelRoutedEventArgs
            RaiseEvent(new CancelRoutedEventArgs(PreviewSelectedItemChangedEvent, e));
        }
        shouldCancel = e.Cancel;
    }
}

以下是一些扩展方法,用于支持TreeView查找鼠标下的对象。

public static class ItemContainerExtensions
{
    // get the object that exists in the container at the specified point.
    public static object GetObjectAtPoint<ItemContainer>(this ItemsControl control, Point p)
        where ItemContainer : DependencyObject
    {
        // ItemContainer - can be ListViewItem, or TreeViewItem and so on(depends on control)
        ItemContainer obj = GetContainerAtPoint<ItemContainer>(control, p);
        if (obj == null)
            return null;

        // it is worth noting that the passed _control_ may not be the direct parent of the
        // container that exists at this point. This can be the case in a TreeView, where the
        // parent of a TreeViewItem may be either the TreeView or a intermediate TreeViewItem
        ItemsControl parentGenerator = obj.GetParentItemsControl( );

        // hopefully this isn't possible?
        if (parentGenerator == null)
            return null;

        return parentGenerator.ItemContainerGenerator.ItemFromContainer(obj);
    }

    // use the VisualTreeHelper to find the container at the specified point.
    public static ItemContainer GetContainerAtPoint<ItemContainer>(this ItemsControl control, Point p)
        where ItemContainer : DependencyObject
    {
        HitTestResult result = VisualTreeHelper.HitTest(control, p);
        DependencyObject obj = result.VisualHit;

        while (VisualTreeHelper.GetParent(obj) != null && !(obj is ItemContainer))
        {
            obj = VisualTreeHelper.GetParent(obj);
        }

        // Will return null if not found
        return obj as ItemContainer;
    }

    // walk up the visual tree looking for the nearest ItemsControl parent of the specified
    // depObject, returns null if one isn't found.
    public static ItemsControl GetParentItemsControl(this DependencyObject depObject)
    {
        DependencyObject obj = VisualTreeHelper.GetParent(depObject);
        while (VisualTreeHelper.GetParent(obj) != null && !(obj is ItemsControl))
        {
            obj = VisualTreeHelper.GetParent(obj);
        }

        // will return null if not found
        return obj as ItemsControl;
    }
}

最后,但同样重要的是使用路由事件子系统的自定义EventArgs。
public class CancelRoutedEventArgs : RoutedEventArgs
{
    private readonly CancelEventArgs _CancelArgs;

    public CancelRoutedEventArgs(RoutedEvent @event, CancelEventArgs cancelArgs)
        : base(@event)
    {
        _CancelArgs = cancelArgs;
    }

    // override the InvokeEventHandler because we are going to pass it CancelEventArgs
    // not the normal RoutedEventArgs
    protected override void InvokeEventHandler(Delegate genericHandler, object genericTarget)
    {
        CancelEventHandler handler = (CancelEventHandler)genericHandler;
        handler(genericTarget, _CancelArgs);
    }

    // the result
    public bool Cancel
    {
        get
        {
            return _CancelArgs.Cancel;
        }
    }
}

2

不要选择“已选择/未选择”,更好的方法是钩入PreviewMouseDown。处理“已选择”和“未选择”事件的问题在于,当您收到通知时,事件已经发生了。因为它已经发生了,所以没有什么可以取消的。

另一方面,预览事件是可取消的。虽然这不是您想要的确切事件,但它确实为您提供了防止用户选择不同节点的机会。


9
当他们使用键盘、触摸屏或手写笔时,我需要处理这些吗? - Ray
@Ray 将 TreeView.Focusable 属性设置为 false,这样就不再可能使用键盘获得焦点了。 - this.myself
@this.myself:但是如果他们应该能够使用键盘呢? - Mikkel K.

2
CAMS_ARIES:
XAML:
代码:
  private bool ManejarSeleccionNodoArbol(Object origen)
    {
        return true;  // with true, the selected nodo don't change
        return false // with false, the selected nodo change
    }


    private void Arbol_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {            
        if (e.Source is TreeViewItem)
        {
            e.Handled = ManejarSeleccionNodoArbol(e.Source);
        }
    }

    private void Arbol_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Source is TreeViewItem)
        {
           e.Handled=ManejarSeleccionNodoArbol(e.Source);
        }
    }         

我们能得到一个英文版吗? - Mark Richman

2

与例如关闭事件(Closing event)不同,您无法取消事件。但是,如果缓存上次选择的值,则可以撤消它。秘诀在于,必须在不重新激发SelectionChanged事件的情况下更改所选内容。以下是一个示例:

    private object _LastSelection = null;
    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (IsUpdated)
        {
            MessageBoxResult result = MessageBox.Show("The current record has been modified. Are you sure you want to navigate away? Click Cancel to continue editing. If you click OK all changes will be lost.", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Hand);
            switch (result)
            {
                case MessageBoxResult.Cancel:
                    e.Handled = true;
                    // disable event so this doesn't go into an infinite loop when the selection is changed to the cached value
                    PersonListView.SelectionChanged -= new SelectionChangedEventHandler(OnSelectionChanged);
                    PersonListView.SelectedItem = _LastSelection;
                    PersonListView.SelectionChanged += new SelectionChangedEventHandler(OnSelectionChanged);
                    return;
                case MessageBoxResult.OK:
                    // revert the object to the original state
                    LocalDataContext.Persons.GetOriginalEntityState(_LastSelection).CopyTo(_LastSelection);
                    IsUpdated = false;
                    Refresh();
                    break;
                default:
                    throw new ApplicationException("Invalid response.");
            }
        }

        // cache the selected item for undo
        _LastSelection = PersonListView.SelectedItem;
    }

1
我尝试过类似这样的东西(但是使用TreeView而不是您示例中的列表视图)。并且在我重新启用事件之后,它会再次触发,尝试更改为新项目。 - Ray
1
此外,对于TreeView,您不能像那样设置所选项,您需要在该项上设置IsSelected。 - Ray

1

SelectedItemChanged事件在SelectedItem已经改变后才被触发,此时你无法真正地取消该事件。

你可以做的是监听鼠标点击并在SelectedItem被更改之前取消它们。


1

您可以创建一个从TreeView派生的自定义控件,然后覆盖OnSelectedItemChanged方法。

在调用基类之前,您可以首先使用CancelEventArgs参数触发自定义事件。如果参数.Cancel变为true,则不要调用基类,而是选择旧项(请注意,OnSelectedItemChanged将再次被调用)。

这并不是最好的解决方案,但至少它保持了树控件内部的逻辑,并且没有机会触发比所需更多的选择更改事件。此外,您不需要关心用户是单击树、使用键盘还是可能以编程方式更改选择。


0

我解决了一个树形视图和一次只显示一个文档的问题。这个解决方案基于一个可附加的行为,可以附加到普通的树形视图上:

<TreeView Grid.Column="0"
       ItemsSource="{Binding TreeViewItems}"
       behav:TreeViewSelectionChangedBehavior.ChangedCommand="{Binding SelectItemChangedCommand}"
       >
     <TreeView.ItemTemplate>
         <HierarchicalDataTemplate ItemsSource="{Binding Children}">
             <StackPanel Orientation="Horizontal">
                 <TextBlock Text="{Binding Name}"
                         ToolTipService.ShowOnDisabled="True"
                         VerticalAlignment="Center" Margin="3" />
             </StackPanel>
         </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
     <TreeView.ItemContainerStyle>
         <Style TargetType="{x:Type TreeViewItem}">
             <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
             <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
         </Style>
     </TreeView.ItemContainerStyle>
</TreeView>

行为的代码如下:

/// <summary>
/// Source:
/// https://dev59.com/J0jSa4cB1Zd3GeqPCyEx
/// http://social.msdn.microsoft.com/Forums/de-DE/wpf/thread/21bed380-c485-44fb-8741-f9245524d0ae
/// 
/// Attached behaviour to implement the SelectionChanged command/event via delegate command binding or routed commands.
/// </summary>
public static class TreeViewSelectionChangedBehavior
{
#region fields
/// <summary>
/// Field of attached ICommand property
/// </summary>
private static readonly DependencyProperty ChangedCommandProperty = DependencyProperty.RegisterAttached(
  "ChangedCommand",
  typeof(ICommand),
  typeof(TreeViewSelectionChangedBehavior),
  new PropertyMetadata(null, OnSelectionChangedCommandChange));

/// <summary>
/// Implement backing store for UndoSelection dependency proeprty to indicate whether selection should be
/// cancelled via MessageBox query or not.
/// </summary>
public static readonly DependencyProperty UndoSelectionProperty =
  DependencyProperty.RegisterAttached("UndoSelection",
  typeof(bool),
  typeof(TreeViewSelectionChangedBehavior),
  new PropertyMetadata(false, OnUndoSelectionChanged));
#endregion fields

#region methods
#region ICommand changed methods
/// <summary>
/// Setter method of the attached ChangedCommand <seealso cref="ICommand"/> property
/// </summary>
/// <param name="source"></param>
/// <param name="value"></param>
public static void SetChangedCommand(DependencyObject source, ICommand value)
{
  source.SetValue(ChangedCommandProperty, value);
}

/// <summary>
/// Getter method of the attached ChangedCommand <seealso cref="ICommand"/> property
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static ICommand GetChangedCommand(DependencyObject source)
{
  return (ICommand)source.GetValue(ChangedCommandProperty);
}
#endregion ICommand changed methods

#region UndoSelection methods
public static bool GetUndoSelection(DependencyObject obj)
{
  return (bool)obj.GetValue(UndoSelectionProperty);
}

public static void SetUndoSelection(DependencyObject obj, bool value)
{
  obj.SetValue(UndoSelectionProperty, value);
}
#endregion UndoSelection methods

/// <summary>
/// This method is hooked in the definition of the <seealso cref="ChangedCommandProperty"/>.
/// It is called whenever the attached property changes - in our case the event of binding
/// and unbinding the property to a sink is what we are looking for.
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void OnSelectionChangedCommandChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  TreeView uiElement = d as TreeView;  // Remove the handler if it exist to avoid memory leaks

  if (uiElement != null)
  {
      uiElement.SelectedItemChanged -= Selection_Changed;

      var command = e.NewValue as ICommand;
      if (command != null)
      {
          // the property is attached so we attach the Drop event handler
          uiElement.SelectedItemChanged += Selection_Changed;
      }
  }
}

/// <summary>
/// This method is called when the selection changed event occurs. The sender should be the control
/// on which this behaviour is attached - so we convert the sender into a <seealso cref="UIElement"/>
/// and receive the Command through the <seealso cref="GetChangedCommand"/> getter listed above.
/// 
/// The <paramref name="e"/> parameter contains the standard EventArgs data,
/// which is unpacked and reales upon the bound command.
/// 
/// This implementation supports binding of delegate commands and routed commands.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void Selection_Changed(object sender, RoutedPropertyChangedEventArgs<object> e)
{
  var uiElement = sender as TreeView;

  // Sanity check just in case this was somehow send by something else
  if (uiElement == null)
      return;

  ICommand changedCommand = TreeViewSelectionChangedBehavior.GetChangedCommand(uiElement);

  // There may not be a command bound to this after all
  if (changedCommand == null)
      return;

  // Check whether this attached behaviour is bound to a RoutedCommand
  if (changedCommand is RoutedCommand)
  {
      // Execute the routed command
      (changedCommand as RoutedCommand).Execute(e.NewValue, uiElement);
  }
  else
  {
      // Execute the Command as bound delegate
      changedCommand.Execute(e.NewValue);
  }
}

/// <summary>
/// Executes when the bound boolean property indicates that a user should be asked
/// about changing a treeviewitem selection instead of just performing it.
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void OnUndoSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  TreeView uiElement = d as TreeView;  // Remove the handler if it exist to avoid memory leaks

  if (uiElement != null)
  {
      uiElement.PreviewMouseDown -= uiElement_PreviewMouseDown;

      var command = (bool)e.NewValue;
      if (command == true)
      {
          // the property is attached so we attach the Drop event handler
          uiElement.PreviewMouseDown += uiElement_PreviewMouseDown;
      }
  }
}

/// <summary>
/// Based on the solution proposed here:
/// Source: https://dev59.com/_XnZa4cB1Zd3GeqPsaCV
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void uiElement_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  // first did the user click on a tree node?
  var source = e.OriginalSource as DependencyObject;
  while (source != null && !(source is TreeViewItem))
      source = VisualTreeHelper.GetParent(source);

  var itemSource = source as TreeViewItem;
  if (itemSource == null)
      return;

  var treeView = sender as TreeView;
  if (treeView == null)
      return;

  bool undoSelection = TreeViewSelectionChangedBehavior.GetUndoSelection(treeView);
  if (undoSelection == false)
      return;

  // Cancel the attempt to select an item.
  var result = MessageBox.Show("The current document has unsaved data. Do you want to continue without saving data?", "Are you really sure?",
                               MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

  if (result == MessageBoxResult.No)
  {
      // Cancel the attempt to select a differnet item.
      e.Handled = true;
  }
  else
  {
      // Lets disable this for a moment, otherwise, we'll get into an event "recursion"
      treeView.PreviewMouseDown -= uiElement_PreviewMouseDown;

      // Select the new item - make sure a SelectedItemChanged event is fired in any case
      // Even if this means that we have to deselect/select the one and the same item
      if (itemSource.IsSelected == true )
          itemSource.IsSelected = false;

      itemSource.IsSelected = true;

      // Lets enable this to get back to business for next selection
      treeView.PreviewMouseDown += uiElement_PreviewMouseDown;
  }
}
#endregion methods
}

在这个例子中,我展示了一个阻塞消息框,以便在PreviewMouseDown事件发生时阻止它。然后处理该事件以表示选择被取消或者如果未处理,则让treeview本身通过选择要选择的项来处理该事件。

如果用户决定继续(PreviewMouseDown事件未由附加行为处理并调用绑定命令),则该行为会在viewmodel中调用绑定命令。

我想显示消息框可能可以用其他方式完成,但我认为在此处阻止事件发生是至关重要的,因为否则无法取消它(?)。因此,我唯一能想到改进这段代码的方法就是绑定一些字符串使得显示的消息可配置。

我写了一篇文章,其中包含可下载的示例,因为这是一个难以解释的领域(必须对缺失部分做出很多假设,而这些假设可能不会被所有读者共享)。

这里有一篇包含我的结果的文章: http://www.codeproject.com/Articles/995629/Cancelable-TreeView-Navigation-for-Documents-in-WP

请评论这个解决方案,并让我知道是否有改进的空间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接