开发者

MVVM Light EventToCommand doesn't work inside WP7 Pivot DataTemplate

开发者 https://www.devze.com 2023-01-22 18:53 出处:网络
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 regu

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消