Click here to Skip to main content
15,879,535 members

List and checked list box in C# and WPF

vishal_h asked:

Open original thread
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; }
        }

    }
}
Tags: Visual Studio (Visual Studio 2010), WPF

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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