2015-12-10 122 views
0

我没有为此使用任何特定的库。我有页面中的列表视图和点击我想调用一个名为DemoCommand的命令,但是当我将ViewModel绑定到页面时,它显示错误命令未找到。我的Xaml代码是。未找到绑定命令错误

<Grid DataContext="{Binding OrdersObject}"> 
    <ListView Grid.Row="1" ItemsSource="{Binding data.orders}" SelectionChanged="ListView_SelectionChanged" ItemTemplate="{StaticResource DueOrderTemplate}" ItemContainerStyle="{StaticResource StretchItemStyle}"> 
     <Interactivity:Interaction.Behaviors> 
      <Core:EventTriggerBehavior EventName="SelectionChanged"> 
       <Core:InvokeCommandAction CommandParameter="{Binding}" Command="{Binding DemoCommand}"/> 
      </Core:EventTriggerBehavior> 
     </Interactivity:Interaction.Behaviors> 
    </ListView> 
</Grid> 

我的绑定代码是

var OrdersObj = new ViewModels.OrdersVM(); 

      await OrdersObj.GetOrders("cancel"); 
      this.DataContext = OrdersObj; 

我的视图模型的代码是

class OrdersVM 
{ 
    public Models.DueOrderM OrdersObject { get; set; } 

    async public Task<Boolean> GetOrders(string order_type) 
    { 
     OrdersObject=//from API 
    } 
    RelayCommand<Models.OOrderM> _demoCommand; 

    public RelayCommand<Models.OOrderM> DemoCommand 
    { 
     get 
     { 
      if (_demoCommand == null) 
      { 
       _demoCommand = new RelayCommand<Models.OOrderM>((itemParam) => 
       { 
        System.Diagnostics.Debug.WriteLine(itemParam); 
       }); 
      } 
      return _demoCommand; 
     } 
     set { _demoCommand = value; } 
    } 

}

回答

1

我能找几个例子,但均未工作,我根据我只需要改变我的XAML代码得到了解决自己后,自己解决问题。更新后的代码是

<ListView x:Name="lstItem" Grid.Row="1" ItemsSource="{Binding OrdersObject.data.orders}" ItemTemplate="{StaticResource DueOrderTemplate}" ItemContainerStyle="{StaticResource StretchItemStyle}"> 
     <Interactivity:Interaction.Behaviors> 
      <Core:EventTriggerBehavior EventName="SelectionChanged"> 
       <Core:InvokeCommandAction CommandParameter="{Binding SelectedItem, ElementName=lstItem}" Command="{Binding DemoCommand}"/> 
      </Core:EventTriggerBehavior> 
     </Interactivity:Interaction.Behaviors> 
    </ListView> 

我注意到,在命令参数被命名列表视图中,需要用x为其指定重要的东西:名称,然后它的工作别人没有奏效。希望它能帮助像我这样的人。

0
  1. 我会成立DataContext没有 '后台代码',但在XAML

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    DataContext="{Binding ViewModel, Source={StaticResource ViewModelLocator}}" 
    d:DataContext="{Binding ViewModel, Source={StaticResource ViewModelLocator}}" 
    
  2. 您设置OrdersObjectDataContext到电网,所以尽量RelativeSources所以你的命令绑定可以再次看到视图模型。

+0

因为我需要从API获取数据,所以我需要先调用该方法,但我无法在Xaml中找到这种方法,这就是为什么我这样做的原因。 – Rohit

+0

我无法实现它。你可以请分享一个代码示例。 – Rohit

+0

通过nuget将MVVM Light添加到您的项目中,您将获得ViewModelLocator的示例实现,并且它是有据可查的。 –

相关问题