WPF中的清单框 [英] checklist box in WPF

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

问题描述

单击添加按钮时,我想在WPF窗口的文本框中输入的复选框列表中添加项目..
添加了项目,但显示了文本,但是新项目覆盖了上一个项目,但是我想在上一个项目旁边添加新项目..
谁能帮我...

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...

<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文件
-----------




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

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

   }





public class TripInfo

   {

        public TripInfo(bool isVisited, string cityName)

          {

             IsVisited = isVisited;

             CityName = cityName;

        }


         public Boolean IsVisited

          { get; set; }


        public String CityName

        { get; set; }
     }

 }

推荐答案

哦,现在我知道您犯了什么错误.

每次单击时都在设置ItemsSource,并且使用了一个列表,该列表可以替换为ObservableCollection,该列表可以在您更改集合时通知您.
用下面要说明的方式,您不必一次又一次地设置ItemsSource.

可以这样解决:

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:

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! :)


我已经修改了您的代码,并且在下面指定了工作版本

I have modified your code and the working version is specified below

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();

    }
}


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

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