clear combobox items in wpf

Page 1 of 1

7 Replies - 6516 Views - Last Post: 06 November 2013 - 04:01 AM Rate Topic: -----

#1 Deep_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 13

clear combobox items in wpf

Posted 05 November 2013 - 01:18 AM

Hi,

I have a combobox in wpf.i want to clear the items dynamically.I tried combobox.Items.Clear().But it's not working and gives an error.please help me to solve this.....

Is This A Good Question/Topic? 0
  • +

Replies To: clear combobox items in wpf

#2 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: clear combobox items in wpf

Posted 05 November 2013 - 01:47 AM

If you have a combobox in WPF, it should be BOUND to a datasource. That means when you clear the source, the combobox will clear. You shouldn't be writing WPF applications the same as WinForms ones; you should be using the MVVM pattern. Take a look at this tutorial to get you started.
Was This Post Helpful? 0
  • +
  • -

#3 Deep_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 13

Re: clear combobox items in wpf

Posted 05 November 2013 - 02:15 AM

View PostMrShoes, on 05 November 2013 - 01:47 AM, said:

If you have a combobox in WPF, it should be BOUND to a datasource. That means when you clear the source, the combobox will clear. You shouldn't be writing WPF applications the same as WinForms ones; you should be using the MVVM pattern. Take a look at this tutorial to get you started.


Thanks for your reply.I'm not using databinding to that.I'm using Items.Add() method.So that's why i used Items.Clear() method
Was This Post Helpful? 0
  • +
  • -

#4 Deep_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 13

Re: clear combobox items in wpf

Posted 05 November 2013 - 10:36 PM

ok fine.Now i changed the way i'm doing to Object list.I'm adding data to list.After that combobox.DataContext=list; Values are showing in the combobox.It's showing an combobox object not set to an instance of an object exception.why is that?
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: clear combobox items in wpf

Posted 06 November 2013 - 02:21 AM

At what point are you getting a Null reference exception? If you want to clear the items in the combobox, are you just clearing the list? Or deleting it? Have you considered removing the binding if you don't want the items to show any more?
Was This Post Helpful? 0
  • +
  • -

#6 Deep_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 13

Re: clear combobox items in wpf

Posted 06 November 2013 - 02:48 AM

Now at the moment i want to show the items through the items list.But it's showing the one word as seperate letters.As an example for name "John",it's showing the name as,
J
o
h
n
like that.why is that?

and when starts the program it gives that null exception.
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: clear combobox items in wpf

Posted 06 November 2013 - 03:49 AM

It's hard to tell without seeing code, but I'd guess that all the items are strings, and a string is an array of char types, so it's considering each char as an item in the collection. Not sure about the null ref exception. How are you populating the list and binding it?
Was This Post Helpful? 0
  • +
  • -

#8 Deep_Coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-October 13

Re: clear combobox items in wpf

Posted 06 November 2013 - 04:01 AM

View PostMrShoes, on 06 November 2013 - 03:49 AM, said:

It's hard to tell without seeing code, but I'd guess that all the items are strings, and a string is an array of char types, so it's considering each char as an item in the collection. Not sure about the null ref exception. How are you populating the list and binding it?


yeah i was also thought about that.i have created a public class.from that class object i'm creating list object.Example,

public class name{
public string name{get;set;}
}

------------------------------------
from that,
List<name> na=new List<name>();
na.Add(new name { name = firstName });
------------------------------------
finally,
combobox.DataContext=na;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1