locked
Execute a command on ListView MVVM RRS feed

  • Question

  • Hello Everybody !!

    I'm trying to execute a command on ListView for getting the selected item.

    Here is my 1st XAML Code : 

       <ListView ItemsSource="{Binding AzardsCollection}" 
                          MaxHeight="650"
                          IsItemClickEnabled="True"
                          x:Name="HazardListView"
                          SelectionMode="Single"
                          helpers:ItemClickCommandHelper.Command="{Binding HazardSelectedChangedCommand}"
                          ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Auto">
    
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Stretch" />
    
                            <Setter Property="Padding"
                                    Value="0" />
                            <Setter Property="Margin"
                                    Value="-5, 0, 10, 15" />
    
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Name}"
                                      RequestedTheme="Light"
                                      Style="{StaticResource CheckBoxStyleBlue}"
                                      IsChecked="{Binding IsSelected, Mode=TwoWay}">
                                
    
                            </CheckBox>
    
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    Here is my 2nd XAML Code : 

     <ListView ItemsSource="{Binding AzardsCollection}" 
                          MaxHeight="650"
                          IsItemClickEnabled="True"
                          x:Name="HazardListView"
                          SelectionMode="Single"
                          
                          ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollMode="Auto">
                    <i:Interaction.Behaviors>
                        <core:EventTriggerBehavior EventName="Tapped">
                            <core:InvokeCommandAction Command="{Binding HazardSelectedChangedCommand}"
                                                       />
                        </core:EventTriggerBehavior>
                    </i:Interaction.Behaviors>
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment"
                                    Value="Stretch" />
    
                            <Setter Property="Padding"
                                    Value="0" />
                            <Setter Property="Margin"
                                    Value="-5, 0, 10, 15" />
    
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Name}"
                                      RequestedTheme="Light"
                                      Style="{StaticResource CheckBoxStyleBlue}"
                                      IsChecked="{Binding IsSelected, Mode=TwoWay}">
                                
    
                            </CheckBox>
    
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

    My second code launch me a cast exception.

    My objectif is to retrieve the value of selected checkbox on my ListView

    Someone has an idea please?

    Thanks

    Tuesday, October 6, 2015 3:31 PM

Answers

  • Please refer to my reply in the following thread: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/5be56c0f-094e-4601-b50d-672ffd787eb1/execute-a-command-on-checkboxmvvm?forum=wpdevelop#5be56c0f-094e-4601-b50d-672ffd787eb1. And please don't ask the same question twice.

    >>The problem is that when the check box is checked the command isn't launched.

    Handle the ItemClick event and change the type argument of the RelayCommand to object (or whatever type you are returning from your converter as per my reply in the other thread):

    RelayCommand<object> _hazardSelectedChangedCommand;
            public ICommand HazardSelectedChangedCommand
            {
                get
                {
                    return _hazardSelectedChangedCommand ?? (_hazardSelectedChangedCommand = new RelayCommand<object>(HazardSelectedChanged));
                }
            }

     

    <core:EventTriggerBehavior EventName="ItemClick">
                            <core:InvokeCommandAction Command="{Binding HazardSelectedChangedCommand}"
                                                       />
                        </core:EventTriggerBehavior>

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Barry Wang Tuesday, October 13, 2015 1:49 AM
    • Marked as answer by Barry Wang Tuesday, October 20, 2015 1:47 AM
    Wednesday, October 7, 2015 1:57 PM

All replies

  • Hello DiddyRennes,

    The idea seems to be this, there is a bool value to indicate whether to Execute or not Execute the command in your code. But the problem is that we don't know other part of your code, for example, how you write your command in code behind. Your two code parts does not mention this.

    Best regards,

    Barry


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 7, 2015 10:10 AM
  • Hello Barry!

    The problem is that the command doesn't work.

    Here is my command code : 

            public ICommand HazardSelectedChangedCommand
            {
                get
                {
                    return _hazardSelectedChangedCommand ?? (_hazardSelectedChangedCommand = new RelayCommand<string>(HazardSelectedChanged));
                }
            }

    The problem is that when the check box is checked the command isn't launched.

    Do you have an other idea?

    Wednesday, October 7, 2015 12:26 PM
  • Please refer to my reply in the following thread: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/5be56c0f-094e-4601-b50d-672ffd787eb1/execute-a-command-on-checkboxmvvm?forum=wpdevelop#5be56c0f-094e-4601-b50d-672ffd787eb1. And please don't ask the same question twice.

    >>The problem is that when the check box is checked the command isn't launched.

    Handle the ItemClick event and change the type argument of the RelayCommand to object (or whatever type you are returning from your converter as per my reply in the other thread):

    RelayCommand<object> _hazardSelectedChangedCommand;
            public ICommand HazardSelectedChangedCommand
            {
                get
                {
                    return _hazardSelectedChangedCommand ?? (_hazardSelectedChangedCommand = new RelayCommand<object>(HazardSelectedChanged));
                }
            }

     

    <core:EventTriggerBehavior EventName="ItemClick">
                            <core:InvokeCommandAction Command="{Binding HazardSelectedChangedCommand}"
                                                       />
                        </core:EventTriggerBehavior>

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Barry Wang Tuesday, October 13, 2015 1:49 AM
    • Marked as answer by Barry Wang Tuesday, October 20, 2015 1:47 AM
    Wednesday, October 7, 2015 1:57 PM