Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have a WPF application. When I am adding the values from the text box to the checked list box, the values get added properly. But when I add the items in the checked list box from another form, the previous value is overiden and only the new value appears. That is all the previous items are removed and only the current item is displayed.
Thanks
I am also giving my source code..
Plz tell me what I need to do:

first window code
<pre lang="xml"><Window x:Class="WpfApplication9.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="536" Width="853">
    <Grid>
        <TabControl Height="260" HorizontalAlignment="Left" Margin="41,57,0,0" Name="tabControl1" VerticalAlignment="Top" Width="731">
            <TabItem Header="old" Name="tabItem1">
                <Grid>
                    <Grid Height="180" Margin="61,15,333,32" Width="327">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="10*" />
                            <ColumnDefinition Width="317*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="84*" />
                            <RowDefinition Height="81*" />
                        </Grid.RowDefinitions>
                        <TextBox Grid.ColumnSpan="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="8,60,0,0" Name="textBox3" VerticalAlignment="Top" Width="209" />
                        <Button Content="add prev" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="215,6,0,0" Name="btn_AssgnLesson" VerticalAlignment="Top" Width="96" Click="btn_AssgnLesson_Click" />
                        <Button Content="add here" Grid.Column="1" Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="215,57,0,0" Name="btn_ChgLesson" VerticalAlignment="Top" Width="96" Click="btn_ChgLesson_Click" />
                        <ListBox DataContext="{Binding}" Grid.Column="1" Grid.RowSpan="2" Height="128" HorizontalAlignment="Left" Margin="13,6,0,0" Name="listLesson" Selector.IsSelected="{Binding IsChecked, ElementName=checkBox, Mode=TwoWay}" VerticalAlignment="Top" Width="194">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding IsVisited}" />
                                        <TextBlock Margin="2" Foreground="Black" FontSize="14" Text="{Binding LessonName}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                </Grid>
            </TabItem>
            <TabItem Header="new">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition>
                        </ColumnDefinition>
                        <ColumnDefinition>
                        </ColumnDefinition>
                    </Grid.ColumnDefinitions>
                </Grid>
                <!--
                <GroupBox Header="groupBox1" Height="148" Name="groupBox1" Width="277">
                    <ListBox Height="74" Name="listLesson" Width="241" DataContext="{Binding}" IsSelected="{Binding IsChecked, ElementName=checkBox, Mode=TwoWay}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding IsVisited}" />
                                    <TextBlock Margin="2" Foreground="Black" FontSize="14" Text="{Binding LessonName}" />
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </GroupBox>
                -->
            </TabItem>
        </TabControl>
    </Grid>
</Window

>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication9
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<TripInfo> tripList = new List<TripInfo>();
        public MainWindow()
    {
        InitializeComponent();
        listLesson.ItemsSource=tripList;
        //list.ItemsSource = tripList;
    }
    
       
        public class TripInfo

         {

        public TripInfo(bool isVisited, string cityName)

          {

             IsVisited = isVisited;

             LessonName = cityName;

         }


         public Boolean IsVisited

          { get; set; }


         public String LessonName

        { get; set; }
        }

      

        private void btn_AssgnLesson_Click(object sender, RoutedEventArgs e)
        {
            Window1 wd1 = new Window1();
            wd1.Show();
        }

        private void btn_ChgLesson_Click(object sender, RoutedEventArgs e)
        {
            tripList.Add(new TripInfo(false, textBox3.Text));
            listLesson.Items.Refresh();
        }

 
    }
}



second window from enter value
<<pre lang="xml">Window x:Class="WpfApplication9.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Button Content="add lesson" Height="23" HorizontalAlignment="Left" Margin="66,77,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="52,36,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication9
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        MainWindow objmain = new MainWindow();
        List<TripInfo> tripList = new List<TripInfo>();
        public Window1()
        {
             InitializeComponent();
            objmain.listLesson.ItemsSource=tripList;
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

        

            //logic to add value in check list box

            

           objmain.Show();
            tripList.Add(new TripInfo(false, textBox1.Text));
            objmain.listLesson.Items.Refresh();
            this.Hide();
        }
        public class TripInfo

         {

        public TripInfo(bool isVisited, string cityName)

          {

             IsVisited = isVisited;

             LessonName = cityName;

         }


         public Boolean IsVisited

          { get; set; }


         public String LessonName

        { get; set; }
        }

    }
}
Posted
Updated 1-Apr-11 20:50pm
v2

1 solution

Hi,

i think your problem is that you have to lists...in each Window is one List of Type List<tripinfo>.

Declare in Window1 instead of the list a othe property:
public TripInfo tripInfo;


and

fill this Porperty in the Click Event

C#
private void button1_Click(object sender, RoutedEventArgs e)
        {  
            tripInfo = new TripInfo(false, textBox1.Text);           
            this.Hide();

        }


then you access the property in your MainForm on the AssgnLesson Button and say ShowDialog() so Code will Block till you close Window1
C#
private void btn_AssgnLesson_Click(object sender, RoutedEventArgs e)
       {
           Window1 wd1 = new Window1();
           wd1.ShowDialog();
           tripList.Add(wd1.tripInfo);
           listLesson.ItemsSource=tripList;

       }



Thats all but you make your live easier if you set the Itemsource in Xaml and take instaed of List the ObservableCollection


public ObservableCollection<tripinfo> tripList {get;set;}
        
public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }</tripinfo>


In Xaml it looks like

XML
<ListBox ItemSource="{Binding Path=tripList }" Grid.Column="1" Grid.RowSpan="2" Height="128" HorizontalAlignment="Left" Margin="13,6,0,0" Name="listLesson" Selector.IsSelected="{Binding IsChecked, ElementName=checkBox, Mode=TwoWay}" VerticalAlignment="Top" Width="194">
                          <ListBox.ItemTemplate>
                              <DataTemplate>
                                  <StackPanel Orientation="Horizontal">
                                      <CheckBox Margin="2" VerticalAlignment="Center" IsChecked="{Binding IsVisited}" />
                                      <TextBlock Margin="2" Foreground="Black" FontSize="14" Text="{Binding LessonName}" />
                                  </StackPanel>
                              </DataTemplate>
                          </ListBox.ItemTemplate>
                      </ListBox>


Hope it helps

Sorry but i actually can´t send my code trough the compiler and check if syntax etc. is allover corect
 
Share this answer
 
Comments
vishal_h 2-Apr-11 3:42am    
sir but it give me error as
Error 1 The best overloaded method match for 'System.Collections.Generic.List<wpfapplication9.mainwindow.tripinfo>.Add(WpfApplication9.MainWindow.TripInfo)' has some invalid arguments D:\Software Development\vishal_wpf\dem\WpfApplication9\WpfApplication9\MainWindow.xaml.cs 67 13 WpfApplication9
Sandeep Mewara 2-Apr-11 3:53am    
Did you try to resolve it or just copy-pasted things and wanted/expected things to work?
DrNimnull 2-Apr-11 5:08am    
This error occours if the List is of an other Type then the object you will add to the list. That´s only what i can say, because I have no more Infos...
Sandeep Mewara 2-Apr-11 3:52am    
My 5! for the help and answer.
RaviRanjanKr 2-Apr-11 7:05am    
Nice Answer! My 5. :)

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