简体   繁体   English

Windows Phone 7-无法从ViewModel触发事件。

[英]Windows Phone 7 - Can not trigger the event from ViewModel.

I want to write the event for list box from View Model. 我想从视图模型中为列表框编写事件。 I try like this:- 我这样尝试:-

 <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Gray" Padding="5" BorderThickness="1">
                        <StackPanel Orientation="Horizontal">
                            <Border BorderBrush="Wheat" BorderThickness="1">
                                <Image  Name="ListPersonImage" Source="{Binding PersonImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,0,0,0"/>
                            </Border>
                            <TextBlock Text="{Binding FirstName}" Name="firstName" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22"  />

                            <Button DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}" Height="80" Width="80" >
                                <Button.Background>
                                    <ImageBrush  ImageSource="{Binding imagePath,  Converter={StaticResource pathToImageConverter}}" Stretch="Fill" />
                                </Button.Background>
                            </Button>                               
                        </StackPanel>
                    </Border>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Tap">
                            <i:InvokeCommandAction Command="{Binding ItemSelectedCommand,Mode=OneWay}" CommandParameter="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

My ViewModel:- 我的ViewModel:-

  public RelayCommand<MVVMListBoxModel> ItemSelectedCommand { get; private set; }
 public MVVMListBoxViewModel()
        {
           ItemSelectedCommand = new RelayCommand<MVVMListBoxModel>(ItemSelected);
        }

 private void ItemSelected(MVVMListBoxModel myItem)
        {
            MessageBox.Show("Name==>" + myItem.FirstName);
            //throw new NotImplementedException();
        }

But nothing happening. 但是什么也没发生。 Please let me know where I did mistake. 请让我知道我在哪里做错了。 Thanks in advance. 提前致谢。

Check output window to see if you got binding error. 检查输出窗口以查看是否出现绑定错误。 It seems that you got one, because you have ItemSelectedCommand defined in MVVMListBoxViewModel but ListBoxItem 's DataContext is corresponding MVVMListBoxModel , so binding engine couldn't find the command. 看来您有一个,因为您在ItemSelectedCommand定义了MVVMListBoxViewModel但是ListBoxItemDataContext是对应的MVVMListBoxModel ,因此绑定引擎找不到该命令。

Try to move definition of ItemSelectedCommand to MVVMListBoxModel and see if message box get displayed this way. 尝试将ItemSelectedCommand定义ItemSelectedCommand MVVMListBoxModel然后查看消息框是否以这种方式显示。

Do you want the trigger to be upon the SelectionChanged of the Listbox Item? 您是否希望触发器基于Listbox项的SelectionChanged Then the trigger should be outside the <ListBox.ItemTemplate> ... </ListBox.ItemTemplate> . 然后,触发器应该在<ListBox.ItemTemplate> ... </ListBox.ItemTemplate>

And the trigger should bind the CommandParamter to the SelectedItem 并且触发器应将CommandParamter绑定到SelectedItem

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Tap">
        <i:InvokeCommandAction Command="{Binding ItemSelectedCommand,Mode=OneWay}" CommandParameter="{Binding SelectedItem}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ViewModel编辑和保存。 - Editing and saving from a ViewModel. 从View直接调用ViewModel。 可以自行更改DataContext吗? - Direct calls from View to ViewModel. Can DataContext be changed on itself? 我在另一个viewmodel中有一个viewmodel列表。 如何将参数从动作传递到控制器? - I have a list of viewmodel in another viewmodel. How can I pass parameter from action to controller? 复杂的ViewModel。 视图返回一个空的ViewModel。 [HttpPost]动作 - Complex ViewModel. View returns an empty ViewModel. [HttpPost] Action Windows Phone-自定义控件如何将事件传播到ViewModel - Windows Phone - custom control how propagate event to ViewModel Windows Phone 8 Viewmodel绑定 - Windows Phone 8 Viewmodel Binding 如何绑定模型以从Windows Phone应用程序中的ViewModel查看 - how to bind the model to view from viewmodel in windows phone app 如何在Windows Phone 7的bing映射中从ViewModel设置经度和纬度 - How to set the longitude and latitude from ViewModel in bing maps in windows phone 7 我们可以从 ViewModel 向 View 发起事件吗? - Can we raise an event from ViewModel to View? 我们如何使用适用于Windows Phone 8.1的WinRT XAML Toolkit在线系列图表中触发数据点单击事件 - How can we trigger data point click event in lineseries chart using WinRT XAML Toolkit for Windows Phone 8.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM