C#和WPF中的列表和选中的列表框 [英] List and checked list box in C# and WPF

查看:84
本文介绍了C#和WPF中的列表和选中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序.当我将文本框中的值添加到选中的列表框中时,将正确添加这些值.但是,当我通过另一种形式在选中的列表框中添加项目时,先前的值将被覆盖并且仅显示新的值.那就是所有先前的项目都将被删除,并且仅显示当前项目.
谢谢
我还提供了我的源代码..
请告诉我我该怎么办:

第一个窗口代码

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; }
        }

    }
}

推荐答案



我认为您的问题是您必须列出...在每个窗口中都是一个类型为List的List< tripinfo> ;.

在Window1中声明一个属性,而不是在列表中声明:
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

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

        }



然后访问AssgnLesson按钮上MainForm中的属性,并说ShowDialog(),这样代码将一直阻塞,直到您关闭Window1



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;

       }




仅此而已,但如果您在Xaml中设置Itemsource并使用List the ObservableCollection的清单,就可以使生活变得更轻松





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>



在Xaml中看起来像



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


这篇关于C#和WPF中的列表和选中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆