MVVM好的做法?

时间:2014-11-14 06:29:13

标签: c# wpf mvvm treeview

我有一个treeView(FunctionTreeView),想要在View和ViewModel之间交换数据。

这是我的观点

private void FunctionTreeView_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        TreeViewItem parent;
        int pIndex,cIndex = 0;
        if (e.Key == Key.Enter)
            EnteredKeys.Length = 0;
        else
            EnteredKeys.Append(e.Key.ToString());

        if (FunctionTreeView.Items.IndexOf(FunctionTreeView.SelectedItem) != -1)
        {
            //if selected is parent, get parent item. 
            parent = FunctionTreeView.Tag as TreeViewItem;
            cIndex = -1;
        }
        else
        {
            TreeViewItem child = FunctionTreeView.Tag as TreeViewItem;
            parent = ItemsControl.ItemsControlFromItemContainer(child) as TreeViewItem;
            cIndex = parent.ItemContainerGenerator.IndexFromContainer(child);
        }

        pIndex = FunctionTreeView.ItemContainerGenerator.IndexFromContainer(parent);
        ViewModel.PerformSearch(pIndex,cIndex);

PerformSearch函数驻留在ViewModel中。在View中访问这样的UI元素是一种好习惯/最好的方法吗?我是MVVM的新手。

1 个答案:

答案 0 :(得分:0)

public class RelayCommand : ICommand
{
    //Your Code
}

阅读以下链接。它会对你有所帮助。

Basic MVVM and ICommand usuage example

Commands in MVVM