WPF MVVM设计问题

发布于 2024-11-01 03:49:48 字数 778 浏览 3 评论 0原文

在我的视图中,我有一个带有事件“TreeView_MouseLeftButtonDown”的TreeView。如果它触发,则证明鼠标是否单击了 TreeViewItem。如果没有,它将取消选择最后一个 TreeViewItem。 我的问题是,我应该在代码隐藏中执行此操作还是在视图模型类中调用静态方法?你会如何解决这个问题?

方法:

private void treeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (sender != null)
    {
        var treeView = sender as TreeView;
        if (treeView != null && treeView.SelectedItem != null)
            TreeViewHelper.ReturnTreeViewItem(ref treeView, (XmlNode)treeView.SelectedItem).IsSelected = false;
    }
} 

XAML:

<TreeView ... KeyDown="TreeView_KeyDown" 
              MouseLeftButtonDown="TreeView_MouseLeftButtonDown" 
              SelectedItemChanged="TreeView_SelectedItemChanged" />

In my View I have a TreeView with a event "TreeView_MouseLeftButtonDown". If it fires it proofs if the mouse clicked on a TreeViewItem. If not it deselects the last TreeViewItem.
My question is, should i do this in the code-behind or call a static methode in the viewmodel-class? How would you solve this?

The Methode:

private void treeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (sender != null)
    {
        var treeView = sender as TreeView;
        if (treeView != null && treeView.SelectedItem != null)
            TreeViewHelper.ReturnTreeViewItem(ref treeView, (XmlNode)treeView.SelectedItem).IsSelected = false;
    }
} 

XAML:

<TreeView ... KeyDown="TreeView_KeyDown" 
              MouseLeftButtonDown="TreeView_MouseLeftButtonDown" 
              SelectedItemChanged="TreeView_SelectedItemChanged" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-11-08 03:49:48

您正在尝试向 TreeView 中添加行为

我实现这一点的方法是使用附加属性。我将创建一个名为 VerizesLeftClick 或类似的附加属性,并在其中实现逻辑。这样您就不需要在后面的代码中添加事件。

有关示例,请参阅此处

You are trying to add a behaviour to the TreeView.

The way I would implement this would be using Attached Properties. I would create an attached property called VerifiesLeftClick or similar and implement the logic in there. This way you do not need an event in the code behind.

See here for samples.

记忆里有你的影子 2024-11-08 03:49:48

我使用附加行为为您制作了解决方案,这里对此进行了很好的描述 WPF 中的附加行为简介Josh Smith

我的解决方案:

public static class TreeViewBehavior
{
    public static bool GetIsResetMouseLeftButtonDown(TreeView treeView)
    {
        return (bool)treeView.GetValue(IsResetMouseLeftButtonDownProperty);
    }
    public static void SetIsResetMouseLeftButtonDown(TreeView treeViewItem, bool value)
    {
        treeViewItem.SetValue(IsResetMouseLeftButtonDownProperty, value);
    }
    public static readonly DependencyProperty IsResetMouseLeftButtonDownProperty =
        DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown", typeof(bool), typeof(TreeViewBehavior),
        new UIPropertyMetadata(false, OnIsMouseLeftButtonDownChanged));
    static void OnIsMouseLeftButtonDownChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeView item = depObj as TreeView;
        if (item == null)
            return;
        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
        {
            item.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
        }
        else
        {
            item.MouseLeftButtonDown -= OnMouseLeftButtonDown;
        }
    }
    static void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
    {
        var tempItem = e.Source as TreeViewItem;
        if (tempItem != null && tempItem.IsSelected == false)
        {
            tempItem.IsSelected = true;
        }
        else
        {
            var tree = e.Source as TreeView;
            if (tree != null && tree.SelectedItem != null)
            {
                var selItem = (tree.SelectedItem as TreeViewItem);
                if (selItem != null)
                {
                    selItem.IsSelected = false;
                }
            }
        }
    }
}

然后在视图中您应该添加以下内容:

<TreeView local:TreeViewBehavior.IsResetMouseLeftButtonDown="True">

我希望我的解决方案能够实现您想要实现的目标。

I made for you solution using attached behaviors which were pretty well described here Introduction to Attached Behaviors in WPF by Josh Smith

My solution:

public static class TreeViewBehavior
{
    public static bool GetIsResetMouseLeftButtonDown(TreeView treeView)
    {
        return (bool)treeView.GetValue(IsResetMouseLeftButtonDownProperty);
    }
    public static void SetIsResetMouseLeftButtonDown(TreeView treeViewItem, bool value)
    {
        treeViewItem.SetValue(IsResetMouseLeftButtonDownProperty, value);
    }
    public static readonly DependencyProperty IsResetMouseLeftButtonDownProperty =
        DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown", typeof(bool), typeof(TreeViewBehavior),
        new UIPropertyMetadata(false, OnIsMouseLeftButtonDownChanged));
    static void OnIsMouseLeftButtonDownChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeView item = depObj as TreeView;
        if (item == null)
            return;
        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
        {
            item.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
        }
        else
        {
            item.MouseLeftButtonDown -= OnMouseLeftButtonDown;
        }
    }
    static void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
    {
        var tempItem = e.Source as TreeViewItem;
        if (tempItem != null && tempItem.IsSelected == false)
        {
            tempItem.IsSelected = true;
        }
        else
        {
            var tree = e.Source as TreeView;
            if (tree != null && tree.SelectedItem != null)
            {
                var selItem = (tree.SelectedItem as TreeViewItem);
                if (selItem != null)
                {
                    selItem.IsSelected = false;
                }
            }
        }
    }
}

and then in View you should add this:

<TreeView local:TreeViewBehavior.IsResetMouseLeftButtonDown="True">

I hope my solution do what you are trying to achieve.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文