问题描述
所以,我偶然发现了一个问题,同时学习MVVM。我有载有我想在执行时,我双击任何的TextBlocks的在TreeView一个动作的TextBlocks一个TreeView。我开始学习行为,而我的行为是如何实现的一个很好的例子,但这个例子并不行为连接到一个ViewModel的。因此,换句话说,如果我双击文本块,我有捕获它的行为类,但我没有任何视图模型执行任何操作。
So I stumbled upon a problem while learning MVVM. I had a TreeView that contained TextBlocks which I wanted to perform an action on when I double clicked any of the TextBlocks in the TreeView. I started to learn about Behaviors, and I have a great example of how a behavior is implemented but the example does not connect the Behavior to a ViewModel at all. So in other words, if I double click on the TextBlock, I have the Behavior class that catches it but I don't have any ViewModel to perform any actions.
有人能花点时间解释这些如何配合?我在回顾这篇文章:
但我似乎没有领会我一直在寻找。
Could someone take a moment and explain how these tie in? I was reviewing this article:http://msdn.microsoft.com/en-us/library/gg430869(v=pandp.40).aspxBut I didn't seem to grasp what I was looking for.
推荐答案
MVVM的概念为我们提供了在WPF应用程序,这意味着在xaml.cs文件中没有更多的code去耦机制。附加的行为是不同的事情。它不与MVVM关系。
MVVM concept provide us a decoupling mechanism in WPF application which means no more code in xaml.cs file. Attached behavior is different thing. It has not relation with MVVM.
但是,因为如果我们有场景中我不能使用MVVM例如选择文本框上双击文本。这是你想要在文本框中添加行为。
But because if we have scenarios where I cant use MVVM e.g. Select the text of TextBox on double click. Which is a behavior you want to add on textbox.
您preFER实现双击功能在xaml.cs文件,因为它不能重复使用,也紧密耦合。
You will prefer implement the double click functionality in xaml.cs file as it not reusable and also tightly coupled.
这就是行为进入画面。我们将创建文本框的行为,并会附加。现在,你可以将这种行为你想尽可能多的控制。
This is where behavior come into picture. We will create the behavior for TextBox and will attach it. Now you can attach this behavior to as many controls you want.
编辑:
如果您正在使用WPF 4.5。你可以看看Markup事件扩展
if you are using WPF 4.5. you can look Markup Extensions for events
如果你想与附加的行为做。创建具有命令依赖属性双击事件的一个附加行为。您双击行为只是抬高命令连接并在XAML绑定视图模型,我希望你知道命令如何。
If you want to do it with attached behavior. Create an attached behavior of double click event which has Command dependency property. Your double click behavior just raise the command attached and in xaml bind the command with viewmodel which I expect you know how.
希望,我能回答你的评论。
Hope, I am able to answer you comment.
这篇关于如何行为,并在的ViewModels有关MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!