Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Im writting a Code in c#, I Have 2 ListView Controls[one is for to store 20 phone numbers and another one is for UserName and Password].

2 ADD Button for add item to the list, when i click button1 item will store in ListView1 and When i click button2 item will store in ListView2[Superate column for username and password].

What I have tried:

How to Write a Coding For this ??
Anyone Knows Please rply me .......
Posted
Updated 2-Dec-19 21:18pm

1 solution

you can start with MVVM popular pattern for WPF, write firstviewmodel where you can define
list for items to store phone numbers and a button command when click to add into that list. i have created sample for you to store ph number as string when you click on command and a txt box to enter new phn 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)
        {
            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
 
Comments
Member 14672509 3-Dec-19 3:55am    
how to write Program for ListView2 ??
Member 14672509 3-Dec-19 4:06am    
Can you Explain me How Xaml code Will be work? I didn't understand that code.
[no name] 3-Dec-19 4:29am    
you can defined one more listview in xmal and bind new list to ItemSource by adding one more row.
xmal code works on binding from your view mode. the main datacontext of mainview is set on cs file and later what ever you bind with child control should be part of that data conntext file. e.g. new xmal with another list view.
<grid>
<grid.columndefinitions>
<columndefinition>
<columndefinition>

<grid.rowdefinitions>
<rowdefinition>
<rowdefinition>

<listview grid.row="0" grid.column="0" itemssource="{Binding ItemList}">
<stackpanel grid.row="1" grid.column="0">
<textbox borderthickness="1" horizontalalignment="Left" width="200" text="{Binding InputText, Mode=TwoWay}">


<listview grid.row="0" grid.column="1" itemssource="{Binding UserList}">
<stackpanel grid.row="1" grid.column="1">
<textbox borderthickness="1" horizontalalignment="Left" width="200" text="{Binding InputText, Mode=TwoWay}">


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