MVVM 中的 Silverlight UserControl 与 RelayCommand- 例子?

发布于 2024-11-19 23:59:37 字数 556 浏览 2 评论 0原文

有人有一个很好的例子来创建用户控件,然后将命令绑定到它吗?

我的问题是将命令转发到 UserControl 中的控件,例如 TreeView 的 Drop 事件。目前尚不清楚,我该怎么做。


两个答案都很有趣,谢谢,他们有帮助,但是...

我想要这样的东西:

用法:

<my:MyControl Command="{Binding XCommand}" CommandParameter="{Binding [Here what?]}"/>

控件包含两个 DependencyProperties、Command 和 CommandParameters,我想将这两个 DependecyProperties 绑定到 TreeView - Drop 事件。我怎样才能做到这一点?因为 CommandParameter 的用法是相反的:不是控件传递参数,而是用户想要一些东西: CommandParameter="{Binding Text, ElementName=DisableCommandTextBox}"

Has somebody a good example to create a UserControl, and then bind commands to it?

My problem is to forward the command to a control in the UserControl, for example to a TreeView's Drop event. It's not clear, how can I do that.


Both answer are interesting and thanks, they help, but...

I want something like that:

The usage:

<my:MyControl Command="{Binding XCommand}" CommandParameter="{Binding [Here what?]}"/>

The control contains the two DependencyProperties, Command and CommandParameters, and I'd like to bind these two DependecyProperties to a TreeView - Drop event. How can I do that? Because the usage of the CommandParameter is reversed: not the control passes a parameter, but the users wants something: CommandParameter="{Binding Text, ElementName=DisableCommandTextBox}"

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

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

发布评论

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

评论(3

梦冥 2024-11-26 23:59:37

您正在尝试将事件转发到您的视图模型。我想您不需要 RelayCommand,但您需要 EventToCommand。我不知道如何绑定树视图事件,但你总是可以尝试一下。

使用 MVVM Light 的示例:
http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

<Rectangle Fill="White"
           Stroke="Black"
           Width="200"
           Height="100">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseEnter">
            <cmd:EventToCommand Command="{Binding TestCommand,
                                          Mode=OneWay}"
               CommandParameter="{Binding Text,
                                  ElementName=MyTextBox,
                                  Mode=OneWay}"
               MustToggleIsEnabledValue="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Rectangle>

You are trying to forward an event to your viewmodel. I guess you don't need RelayCommand but you will need EventToCommand. I don't know about binding the treeview events, but you can always give it a try.

Example using MVVM Light:
http://geekswithblogs.net/lbugnion/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx

<Rectangle Fill="White"
           Stroke="Black"
           Width="200"
           Height="100">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseEnter">
            <cmd:EventToCommand Command="{Binding TestCommand,
                                          Mode=OneWay}"
               CommandParameter="{Binding Text,
                                  ElementName=MyTextBox,
                                  Mode=OneWay}"
               MustToggleIsEnabledValue="True" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Rectangle>
罪#恶を代价 2024-11-26 23:59:37

不幸的是,由于 TreeView 的限制,我们无法将视图模型中的 RelayCommand 直接绑定到 Drop 事件。

不过,这个问题之前已经解决了。看看这个链接 看看它是如何完成的。如果您想要一个更典型的命令绑定示例(可能是按钮),请在下面留下评论,我会将其添加进去。

Unfortunately, we cannot bind a RelayCommand within a viewmodel directly to the Drop event due to the limitations on TreeView.

However, this problem has been previously solved. Take a look at this link to see how it was done. If you'd like an example of a more typical command binding (to a button perhaps), leave a comment below and I'll add that in.

萌︼了一个春 2024-11-26 23:59:37

我只需保留 CommandParameter,然后使用适当的参数在 SelectedItemChanged 事件处理程序中调用该命令。

I just had to leave the CommandParameter, and I call the command in the SelectedItemChanged event handler with the appropiate parameter.

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