WPF:树型视图绑定到一个ICommand [英] WPF: TreeViewItem bound to an ICommand

查看:161
本文介绍了WPF:树型视图绑定到一个ICommand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忙于创建在WPF我的第一个MVVM应用程序。

I am busy creating my first MVVM application in WPF.

基本上我遇到的问题是,我有一个TreeView(System.Windows.Controls.TreeView),我已经把我的WPF窗口,我已经决定,我将绑定到的CommandViewModel项ReadOnlyCollection还,而这些项目包括一个DisplayString,标签和RelayCommand的。

Basically the problem I am having is that I have a TreeView (System.Windows.Controls.TreeView) which I have placed on my WPF Window, I have decide that I will bind to a ReadOnlyCollection of CommandViewModel items, and these items consist of a DisplayString, Tag and a RelayCommand.

在XAML现在,我有我的TreeView和我有成功我必然要ReadOnlyCollection还这样。我可以看到这一点,一切都看起来不错的UI。

Now in the XAML, I have my TreeView and I have successfully bound my ReadOnlyCollection to this. I can view this and everything looks fine in the UI.

现在的问题是,我需要的RelayCommand绑定到树型视图的命令,但是从我所看到的TreeViewItem没有一个命令。这是否强迫我去做它IsSelected属性,甚至后面TreeView_SelectedItemChanged方法或代码有没有办法在WPF神奇地做到这一点?

The issue now is that I need to bind the RelayCommand to the Command of the TreeViewItem, however from what I can see the TreeViewItem doesn't have a Command. Does this force me to do it in the IsSelected property or even in the Code behind TreeView_SelectedItemChanged method or is there a way to do this magically in WPF?

这是我的代码:

<TreeView BorderBrush="{x:Null}" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch">
<TreeView.Items>
    <TreeViewItem
        Header="New Commands"
        ItemsSource="{Binding Commands}"
        DisplayMemberPath="DisplayName"
        IsExpanded="True">
    </TreeViewItem>
</TreeView.Items>



和理想我很乐意只是去:

and ideally I would love to just go:

<TreeView BorderBrush="{x:Null}" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch">
<TreeView.Items>
    <TreeViewItem
        Header="New Trade"
        ItemsSource="{Binding Commands}"
        DisplayMemberPath="DisplayName"
        IsExpanded="True"
        Command="{Binding Path=Command}">
    </TreeViewItem>
</TreeView.Items>



是否有人有一个解决方案,允许我使用的RelayCommand基础设施我。

Does someone have a solution that allows me to use the RelayCommand infrastructure I have.

谢谢你们,非常感谢!

理查德

推荐答案

感谢您的投入问题,是的,我没有说我没有在那个时候要解决的背后守则,然而,我还是很多,我只是失去了一些东西的印象...所以我最终使用的TreeView_SelectedItemChanged事件。

Thanks for the input into the issue, and yes, I did say I didn't want a Code behind solution, however at that time I was still very much under the impression that I was simply missing something... so I ended up using the TreeView_SelectedItemChanged event.

尽管威尔的做法似乎是一个不错的变通,对于我的个人情况,我决定,我会用后面的代码。这样做的原因是为了让查看和XAML将保持,因为这将是如果树型视图不得不到我的指挥才能绑定一个命令属性。现在我没有改变模板或意见,我所要做的就是添加代码,为TreeView_SelectedItemChanged事件

Even though Will's approach seems like a good work around, for my personal situation I decided that I would use the code behind. The reason for this is so that the View and XAML would remain as it would be if the TreeViewItem had a "Command" property to which my Command could be bound. Now I do not have to change the Templates or the Views, all I have to do is add the code and the Event for the TreeView_SelectedItemChanged.

我的解决办法:

  private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        if (sender != null)
        {
            var treeView = sender as TreeView;
            if (treeView != null)
            {
                var commandViewModel = treeView.SelectedItem as CommandViewModel;
                if (commandViewModel != null)
                {
                    var mi = commandViewModel.Command.GetType().GetMethod("Execute");
                    mi.Invoke(commandViewModel.Command, new Object[] {null});
                }
            }
        }
    }



由于我已经有连接到树型视图的RelayCommand,所有我现在做的是刚刚手动调用执行方法在特定RelayCommand。

As I already have the RelayCommand attached to the TreeViewItem, all I am now doing is to just manually invoke the "Execute" method on that specific RelayCommand.

如果这是绕了那么请让我知道...

If this is the completely wrong way of going about it then please let me know...

由于完全错误的方式!

这篇关于WPF:树型视图绑定到一个ICommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆