Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF VS2010
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 1-Apr-11 21:46pm
vishal_h1.8K
Edited 1-Apr-11 21:50pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
i think your problem is that you have to lists...in each Window is one List of Type List.
 
Declare in Window1 instead of the list a othe property:
public TripInfo tripInfo;
 
and
 
fill this Porperty in the Click Event
 
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
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
 
<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
  Permalink  
Comments
vishal_h at 2-Apr-11 3:42am
   
sir but it give me error as
Error 1 The best overloaded method match for 'System.Collections.Generic.List.Add(WpfApplication9.MainWindow.TripInfo)' has some invalid arguments D:\Software Development\vishal_wpf\dem\WpfApplication9\WpfApplication9\MainWindow.xaml.cs 67 13 WpfApplication9
Sandeep Mewara at 2-Apr-11 3:53am
   
Did you try to resolve it or just copy-pasted things and wanted/expected things to work?
DrNimnull at 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 at 2-Apr-11 3:52am
   
My 5! for the help and answer.
RaviRanjankr at 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)

  Print Answers RSS
0 Zoltán Zörgő 300
1 /\jmot 205
2 CHill60 145
3 Peter Leow 145
4 CPallini 135
0 Sergey Alexandrovich Kryukov 9,373
1 OriginalGriff 6,881
2 Peter Leow 4,577
3 Zoltán Zörgő 4,244
4 CHill60 2,892


Advertise | Privacy | Mobile
Web02 | 2.8.150129.1 | Last Updated 2 Apr 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100