Click here to Skip to main content
15,884,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
on click of add button i want to add item in check list box which is enter in text box of WPF window..
item is added but text is display but new item is overrites the previous one but i want to add new item next to previous..
can any one help me...

XML
<window x:class="CheckBox.Window1" xmlns:x="#unknown">
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="Checked Box in ListBox" Height="300" Width="300">
    <grid> 
        <listbox name="list" margin="5,5,5,55" horizontalcontentalignment="Stretch">
            <listbox.itemtemplate>
                <datatemplate>
                    <border margin="3" borderbrush="Brown" borderthickness="2" cornerradius="3">
                        <border.background>                           
                            <lineargradientbrush>                                
                                <gradientstop offset="0" color="Blue" />                               
                                <gradientstop offset="0.5" color="AliceBlue" />                              
                                <gradientstop offset="1" color="Navy" />                               
                            </lineargradientbrush>                        
                        </border.background>        
                        <stackpanel orientation="Horizontal">                            
                            <checkbox margin="2" verticalalignment="Center" ischecked="{Binding IsVisited}" />                           
                            <textblock margin="2" foreground="Yellow" fontsize="14" text="{Binding CityName}" />
                        </stackpanel>                        
                    </border>               
                </datatemplate>        
            </listbox.itemtemplate>          
        </listbox>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="166,225,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <textbox height="23" horizontalalignment="Left" margin="12,226,0,0" name="textBox1" verticalalignment="Top" width="120" />
          </grid>   
</window>



.cs file
-----------

C#
namespace CheckBox

 {

   /// <summary>

   /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

     {

       public Window1()

        {

           InitializeComponent();



       }
       private void button1_Click(object sender, RoutedEventArgs e)
       {
           List<TripInfo> tripList = new List<TripInfo>();


           tripList.Add(new TripInfo(false, textBox1.Text));


        list.ItemsSource = tripList;
       }

   }


C#
public class TripInfo

   {

        public TripInfo(bool isVisited, string cityName)

          {

             IsVisited = isVisited;

             CityName = cityName;

        }


         public Boolean IsVisited

          { get; set; }


        public String CityName

        { get; set; }
     }

 }
Posted
Updated 31-Mar-11 1:48am
v4
Comments
Tarun.K.S 31-Mar-11 5:13am    
Show us the code where you went wrong?
Tarun.K.S 31-Mar-11 7:42am    
You need to show the complete XAML too buddy.

Oh now I know what mistake you did.

You are setting the ItemsSource everytime you click it and you have used a List which can replaced with ObservableCollection which can notify you when your collection changed.
In the way that I am going to explain below, you don't have to set the ItemsSource again and again.

It can be solved in this way:

C#
public partial class Window1 : Window
     {

       private ObservableCollection<tripinfo> trips;
       public Window1()
        {
           InitializeComponent();
           this.Loaded+=new RoutedEventHandler(Window1_Loaded);
        }

       
       public Window1_Loaded(object sender,RoutedEventArgs e)
       {
        trips=new ObservableCollection<tripinfo>();
        trips.Add(new TripInfo(false,"Delhi"));
        trips.Add(new TripInfo(true,"New York"));

        list.ItemsSource=trips;
       }

       private void button1_Click(object sender, RoutedEventArgs e)
       {
           trips.Add(new TripInfo(false, textBox1.Text));
           //No need of setting the ItemsSouce here again. 
           //Let the ObservableCollection do the job!
       }

//Similarly you can delete it using trips.Remove  
   }</tripinfo></tripinfo>



Hope it helped! :)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Apr-11 16:35pm    
Pretty good one, a 5.
--SA
vishal_h 8-Apr-11 3:22am    
how i can read item from that ckeck list box..
I have modified your code and the working version is specified below

C#
public partial class Window1 : Window
{
    List<TripInfo> tripList = new List<TripInfo>();
    public Window1()
    {
        InitializeComponent();
        list.ItemsSource = tripList;
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        tripList.Add(new TripInfo(false, textBox1.Text));
        list.Items.Refresh();

    }
}
 
Share this answer
 
Comments
Tarun.K.S 31-Mar-11 8:04am    
Using Refresh() should be avoided considering the fact that you might have a large collection. I suggest you to use ObservableCollection which can help you notify whenever the collection changes.
Sarath Reviuri 31-Mar-11 8:10am    
Thanks for your comment and i too suggest the solution provided by you.
Tarun.K.S 31-Mar-11 8:18am    
Thanks! :)
vishal_h 1-Apr-11 2:01am    
thank you sir...
Tarun.K.S 1-Apr-11 2:46am    
Don't use refresh() vishal. Oh well as you wish.

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