Search code examples
c#mvvmwindows-phone-8.1icommand

Binding Command Not found Error


I am not using any particular library for this. I have a Listview in page and on its click I want to call a command named DemoCommand but as I bind my ViewModel to page it shows error command not found. My Xaml code is.

<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>

My binding code is

var OrdersObj = new ViewModels.OrdersVM();

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

And my View Model code is

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; }
    }

}


Solution

  • I was able to solve the problem myself after looking few examples none of which were working I got the solution myself according to which I needed to change only my Xaml code. updated code is

    <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>
    

    Important thing I noted was naming of listview in command parameter and need to assign it using x:Name then it worked else it did not work. Hope it helps someone like me.