Click here to Skip to main content
15,889,739 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My Program is to add 20 phone Number to ListView Controls from textbox. I write a below code for add items to listview, But it was not working...

Is my code is Wrong ??

give me a perfect code for add item to listView..

Thnq in Advance

What I have tried:

private void Add_PhoneNumber_Click(object sender, RoutedEventArgs e)
        {

            ListView_PhoneNumber.Items.Add(Textbox_PhoneNumber.Text);
            Textbox_PhoneNumber.Text = " ";
            Textbox_PhoneNumber.Focus();
        }
Posted
Updated 4-Dec-19 5:50am

if remember well from our last discussion in previous question, i have gave you program using MVVM in the same you can improve this case too. OnInsert() will be hot when you click on your button to add ph number.
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ListView Grid.Row="0" ItemsSource="{Binding ItemList}"></ListView>
        <StackPanel Grid.Row="1">
            <TextBox BorderThickness="1" HorizontalAlignment="Left" Width="200" Text="{Binding InputPhnText, Mode=TwoWay}"/>
            <Button HorizontalAlignment="Right" Width="100" Command="{Binding InsertCommand}" Content="Add New Items"/>
        </StackPanel>
    </Grid>


public MainWindow()
      {
          InitializeComponent();
          DataContext = new MainViewModel();
      }



public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            ItemList = new ObservableCollection<string>() { "9999999999" } ;
            InsertCommand = new RelayCommand(OnInsert);
        }

        private void OnInsert(object obj)
        {
// when you click on button this will hit on your view model and add the phn number.
            if (!string.IsNullOrEmpty(InputPhnText))
            {
                ItemList.Add(InputPhnText);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand InsertCommand { get; set; }

        public string InputPhnText{ get; set; }


        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public ObservableCollection<string> ItemList { get; set; }

    }

    public class RelayCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }
 
Share this answer
 
You can use a ListViewItem, see example here: c# - Add Items to Columns in a WPF ListView - Stack Overflow[^]
But this is not the recommended WPF way, see answers here: c# - Add programmatically ListViewItem to Listview in WPF - Stack Overflow[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900