locked
selected item edit and delete relay command

    Question

  • How to selected item Remove from listview Via Relay Command
    MY code is Here
    public class Person
        {
            public String Name { get; set; }

            public int Age { get; set; }

            public String LastName { get; set; }
        }
    //////////////////////

    public class MainPageViewModel
        {
            public ObservableCollection<Person> PersonsList { get; set; }
            public ObservableCollection<Person> SelectedPerson { get; set; }
            public Person PersonToAdd { get; set; }

            public RelayCommand AddPersonCommand { get; set; }
            public RelayCommand RemovePersonCommand { get; set; }
            public RelayCommand TestCommand { get; set; }
            public MainPageViewModel()
            {
                AddPersonCommand = new RelayCommand(AddPerson);
                RemovePersonCommand = new RelayCommand(RemovePerson);
                TestCommand = new RelayCommand(testmsg);
                PersonToAdd = new Person();
                PersonsList = new ObservableCollection<Person>();
            }

            /// <summary>
            /// Remove last person.
            /// </summary>
            private void RemovePerson()
            {
                if (PersonsList.Count > 0)
                {
                    PersonsList.RemoveAt(PersonsList.Count - 1);
                }
            }

            private void AddPerson()
            {
                PersonsList.Add(new Person
                {
                    Name = PersonToAdd.Name,
                    LastName = PersonToAdd.LastName,
                    Age = PersonToAdd.Age,
                });
            }
            private async void testmsg()
            {
                MessageDialog msg = new MessageDialog("Hello Test");
                await msg.ShowAsync();
            }

        }

    /////////////////

    <Page.DataContext>
            <ViewModels:MainPageViewModel>

            </ViewModels:MainPageViewModel>
        </Page.DataContext>

        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <ListView ItemsSource="{Binding PersonsList}"
                     
                      HorizontalAlignment="Left" Margin="44,65,0,327" Width="456">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Width="400" Background="Chocolate">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" FontSize="30" />
                                <TextBlock Text="," FontSize="30" />
                                <TextBlock Text="{Binding LastName}" FontSize="30" />
                            </StackPanel>
                            <TextBlock Text="{Binding Age}" FontSize="30" />
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

            <Button Command="{Binding AddPersonCommand}"
              Content="Add person"
              FontSize="40" Margin="630,447,0,248"/>
            <Button Command="{Binding RemovePersonCommand}"
              Content="Remove person"
              FontSize="40" Margin="913,447,0,248"/>
            <Button Command="{Binding TestCommand}"
                    Content="TestMsg"
                    FontSize="40" Margin="1045,580,0,115" />
            <TextBlock Text="Name" FontSize="30" Margin="633,65,598,640" Height="63"/>
            <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Name, Mode=TwoWay}"
                     FontSize="30" Margin="868,62,80,640"/>
            <TextBlock Text="Last name" FontSize="30" Margin="633,181,551,524"/>
            <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding LastName, Mode=TwoWay}"
                     FontSize="30" Margin="868,181,80,525"/>
            <TextBlock Text="Age" FontSize="30" Margin="633,296,536,400"/>
            <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Age, Mode=TwoWay}"
                     FontSize="30" Margin="868,296,80,403"/>
        </Grid>

    /////////////////
    /// <summary>
        /// A command whose sole purpose is to relay its functionality
        /// to other objects by invoking delegates.
        /// The default return value for the CanExecute method is 'true'.
        /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
        /// <see cref="CanExecute"/> is expected to return a different value.
        /// </summary>
        public class RelayCommand : ICommand
        {
            private readonly Action _execute;
            private readonly Func<bool> _canExecute;

            /// <summary>
            /// Raised when RaiseCanExecuteChanged is called.
            /// </summary>
            public event EventHandler CanExecuteChanged;

            /// <summary>
            /// Creates a new command that can always execute.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            public RelayCommand(Action execute)
                : this(execute, null)
            {
            }

            /// <summary>
            /// Creates a new command.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            /// <param name="canExecute">The execution status logic.</param>
            public RelayCommand(Action execute, Func<bool> canExecute)
            {
                if (execute == null)
                    throw new ArgumentNullException("execute");
                _execute = execute;
                _canExecute = canExecute;
            }

            /// <summary>
            /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
            /// </summary>
            /// <param name="parameter">
            /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
            /// </param>
            /// <returns>true if this command can be executed; otherwise, false.</returns>
            public bool CanExecute(object parameter)
            {
                return _canExecute == null ? true : _canExecute();
            }

            /// <summary>
            /// Executes the <see cref="RelayCommand"/> on the current command target.
            /// </summary>
            /// <param name="parameter">
            /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
            /// </param>
            public void Execute(object parameter)
            {
                _execute();
            }

            /// <summary>
            /// Method used to raise the <see cref="CanExecuteChanged"/> event
            /// to indicate that the return value of the <see cref="CanExecute"/>
            /// method has changed.
            /// </summary>
            public void RaiseCanExecuteChanged()
            {
                var handler = CanExecuteChanged;
                if (handler != null)
                {
                    handler(this, EventArgs.Empty);
                }
            }
        }

    Tuesday, May 20, 2014 5:38 AM

Answers

  • I am not sure how you want to perform the remove.  I am guessing that it is the SelectedPerson to be removed, which would possibly require a loop through the PersonsList to find the correct index, and then use that index in place of (PersonsList.count - 1).

    For editing and saving, try modifying some of the TextBlock into TextBox.  Also, denoting that the binding mode is TwoWay should keep the PersonsList automatically updated with the changes made by the user.

    <ListView ItemsSource="{Binding PersonsList}" HorizontalAlignment="Left" Margin="44,65,0,327" Width="456"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Width="400" Background="Chocolate"> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Name, Mode=TwoWay}" FontSize="30" /> <TextBlock Text="," FontSize="30" /> <TextBox Text="{Binding LastName, Mode=TwoWay}" FontSize="30" /> </StackPanel> <TextBox Text="{Binding Age, Mode=TwoWay}" FontSize="30" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>

    Wednesday, May 21, 2014 10:53 AM

All replies

  • What problem are you having with this? What exactly do you need help with?

    When you have larger than a couple line snippet it would be helpful if you could share a link to a minimal buildable project that demonstrates the problem on your OneDrive.

    Tuesday, May 20, 2014 11:55 PM
    Owner
  • Wednesday, May 21, 2014 3:11 AM
  • Wednesday, May 21, 2014 3:13 AM
  • I want know how to selected item remove , current code is last one remove. And how to selected item edit and save
    Wednesday, May 21, 2014 3:16 AM
  • I am not sure how you want to perform the remove.  I am guessing that it is the SelectedPerson to be removed, which would possibly require a loop through the PersonsList to find the correct index, and then use that index in place of (PersonsList.count - 1).

    For editing and saving, try modifying some of the TextBlock into TextBox.  Also, denoting that the binding mode is TwoWay should keep the PersonsList automatically updated with the changes made by the user.

    <ListView ItemsSource="{Binding PersonsList}" HorizontalAlignment="Left" Margin="44,65,0,327" Width="456"> <ListView.ItemTemplate> <DataTemplate> <StackPanel Width="400" Background="Chocolate"> <StackPanel Orientation="Horizontal"> <TextBox Text="{Binding Name, Mode=TwoWay}" FontSize="30" /> <TextBlock Text="," FontSize="30" /> <TextBox Text="{Binding LastName, Mode=TwoWay}" FontSize="30" /> </StackPanel> <TextBox Text="{Binding Age, Mode=TwoWay}" FontSize="30" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>

    Wednesday, May 21, 2014 10:53 AM