MVVM Light EventToCommand 在 WP7 Pivot DataTemplate 中不起作用

发布于 2024-09-30 10:41:02 字数 1472 浏览 1 评论 0原文

我有一个很奇怪的问题。在我的 WP7 应用程序中,我有一个枢轴控件和一个在其中定义的项目模板(或者在资源中,我尝试了两种方法,但仍然存在同样的问题)。在模板中,我有一个定义了 EventToCommand 的常规按钮(EventName="Click")。我在枢轴外部也有相同的复制粘贴按钮。问题是枢轴外部的按钮工作正常,但内部的按钮不起作用。实际上我注意到,我的枢轴内的任何命令都不起作用。我正在正确处理 ViewModel 中的命令,因为相同的按钮,但在枢轴之外工作得很好。 任何想法可能是什么问题?请帮忙。 提前致谢。 干杯。

PS 我的代码非常标准,但以防万一,它是:

    <controls:Pivot Grid.Row="0"
        x:Name="PivotControl"
        Title="{Binding ApplicationTitle}"                         
        ItemsSource="{Binding BlaBla}">

        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                   <Button Content="Click Me">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <cmd:EventToCommand Command="{Binding MyCommand, Mode=OneWay}" CommandParameterValue="Test"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
           ...

还有 ViewModel 代码:

public RelayCommand<string> MyCommand
    {
        get;
        private set;
    }
//And in the constructor ...
MyCommand= new RelayCommand<string>((param) => HandleTheCommand(param));

...

再次感谢。

I have very strange issue. In my WP7 app I have a pivot control and an item template defined inside it(or in the resources, I have tried both ways, but still same issue). In the template I have a regular button with EventToCommand defined(EventName="Click"). I also have the same copy-pasted button outside the Pivot. The problem is that the button, which is outside the pivot is working ok, but the one inside doesn't work. Actually I have noticed, that any command inside my pivot doesn't work. I am handling correctly the Command in the ViewModel, because the same button, but outside the pivot is working great.
Any ideas what might be the problem? Help, please.
Thanks in advance.
Cheers.

P.S. My code is pretty standard, but just in case here it is:

    <controls:Pivot Grid.Row="0"
        x:Name="PivotControl"
        Title="{Binding ApplicationTitle}"                         
        ItemsSource="{Binding BlaBla}">

        <controls:Pivot.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                   <Button Content="Click Me">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <cmd:EventToCommand Command="{Binding MyCommand, Mode=OneWay}" CommandParameterValue="Test"/>
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>
           ...

And the ViewModel code:

public RelayCommand<string> MyCommand
    {
        get;
        private set;
    }
//And in the constructor ...
MyCommand= new RelayCommand<string>((param) => HandleTheCommand(param));

...

Thanks again.

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

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

发布评论

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

评论(2

心头的小情儿 2024-10-07 10:41:02

因为您位于控件的 ItemTemplate 内部,所以您绑定到的 DataContext 不是您的 ViewModel。绑定 {Binding MyCommand, OneWay} 正在尝试从集合 BlaBla 中的对象上查找属性 MyCommand。这是命令模式的限制之一,因为在 DataTemplates 中,您的 DataContext 通常不是您的 ViewModel。

确实没有什么好的办法。您可以将命令包含在 BlaBla 集合的对象中。您还可以调整自己的触发器,在 VisualTree 中搜索 ViewModel,然后通过反射而不是实际绑定检索命令。

Because you are inside the ItemTemplate of the control the DataContext that you are binding to is not your ViewModel. The Binding {Binding MyCommand, OneWay} is attempting to find the property MyCommand on an object from collection BlaBla. This is one of the limitations of the command pattern in that inside DataTemplates your DataContext is often not your ViewModel.

There really is no good way around it. You could include your command in the objects in the BlaBla collection. You could also right your own trigger that searches up the VisualTree for your ViewModel and then retrieves the command via reflection instead of an actual binding.

z祗昰~ 2024-10-07 10:41:02

silverlight / WP7 的问题是数据上下文不在模板内继承。在使用 Reflector 深入研究 MVVM lite 代码库后,我发现有一个巧妙的技巧可以让它工作。基本上不是在原始场景中公开 ICommand,而是公开 Binding 类型的依赖属性。然后任何实体都可以绑定到此属性,因为绑定可以评估可视化树中的数据上下文。然后创建一个可观察的绑定类,该类基本上具有一个隐藏的附加属性,该属性绑定到 TriggerAction 类中的依赖项属性的 Binding。现在你需要做两件事:
1. 在 TriggerAction 类中,如果 Binding DP 发生变化,则更新 ObservableBinding 成员
2. 在 ObserableBinding 类中,如果隐藏 DP 发生变化,则评估并存储新值。

我希望这也能解决您的问题。

The problem with silverlight / WP7 is that data context is not inherited within templates. After using reflector to dig into MVVM lite codebase I found that there is a nifty trick to make it work. Basically instead of exposing ICommand in the original scenario, expose the dependency property of type Binding. Then any entity can be bound to this property because Binding can evaluate data context from the visual tree. Then create a observable binding class which basically has a hidden attached property bound to the Binding of the dependency property from the TriggerAction class. Now you need to do two things:
1. In TriggerAction class if Binding DP changes, update ObservableBinding member
2. In ObserableBinding class, if hidden DP changes, evaluate and store the new value.

I hope that solves your problem as well.

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