MMattD

Hi, i have a listbox bound to an ObservableCollection of objects, and i want to be able to then have 2 buttons, one that adds items and one that removes the selected item. Is there a way to do this in XAML The real problem is that i have this defined in a DataTemplate or within a style. Here are my current solutions:

1. So i tried to define a method in the Class that contains the collection that adds and removes items, but i would need to then create 2 ObjectDataProviders, and then reference these within the DataTemplate/Style. I'm not quite sure how to do this, because within the data template, how to i create an ObjectDataProvider from an already existing instance of a class

2. I also tried creating 2 Commands (AddListItem, RemoveListItem), and then pass the listbox as the parameter, and then use that to add an item. However this doesn't work, becuase i need to determine what type of object the listbox is bound to, and then create that object by doing something like listBox.Items.Add(new ItemType());

Any suggestions or questions to clarify will be appreciated.


Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

Sam Bent - MSFT

Your second remark hits the essence of the problem. The CLR doesn't provide any simple way to create a new instance of a type T, given only an instance of ObservableCollection<T>. (Or any generic type with parameter T.) WPF doesn't add this feature, either. It's understandable why not: there is no "standard" way to create an instance of a type; the type defines how to create instances, by exposing constructors and factory methods, but there's no requirement that it define any particular constructor or method - not even the default constructor.

The best you can do is use reflection to discover the type T, and call Activator.CreateInstance(itemType). This only works if the type exposes a public default constructor. WPF isn't involved here - this is a pure CLR exercise.






Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

MMattD

The problem here is that I'm starting from the ListBox object in code. So i can access the ListBox.Items which is an ItemsCollection, how can i retrieve the ObservableCollection that the ListBox.Items is bound to




Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

Marlon Grech

Hi there,

May I ask why do you want to do every thing is XAML... Since the collection contains business objects I would say that managing those objects is the business layer job not the UI...

Regards





Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

MMattD

Ok, it's a pretty simple scenario. I have a list within a data template and i want to add two buttons to that data template that add an item to the list, and remove the selected item from the list.

The reason i want to do this from XAML is because i want to have several styles setup to change the way the list looks, ie where the buttons are located, and what not. Perhaps this is a case for a UserControl or custom control Not sure.




Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

Marlon Grech

The fact that you have code not XAML will not stop you from having styles... I can't really understand what you want to achieve. My personnal opinion whould be to do the business logic in your code not in XAML...

That's only my idea please do not get me wrong...





Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

MMattD

Ok, maybe i'm not explaining the situation well, so i'll put it in code, let's say i have a class such as this, and just imagine the guts of INotifyPropertyChanged are filled in...


Code Snippet

public class SomeClass : INotifyPropertyChanged

{

private ObservableCollection<SomeOtherClass> mPeople;

public ObservableCollection<SomeOtherClass> People
{
get...
set...
}

public SomeClass()
{
mNames = new ObservableCollection();
}
}


So it contains a collection of other classes, now, if what you mean by add the business logic to the class is something like adding two methods to the above class such as :

Code Snippet

public class SomeClass : INotifyPropertyChanged

{

..
..

public AddSomeOtherClass()
{
this.mPeople.Add(new SomeOtherClass());
}

public RemoveSomeOtherClass(SomeOtherClass aSomeOtherClass)
{
this.mPeople.Remove(aSomeOtherClass);
}
}

Ok, so here is the problem creating the DataTemplate. In the DataTemplate there will be a ListBox that is bound the to the ObservableCollection, and also two buttons, one to add an item and one to remove an item. How can i connect those buttons up to these methods on the buisness object I would need an ObjectDataProvider, however the instance of the object already exists by the time the DataTemplate is applied...

I hope that explains things a little better.







Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

Marlon Grech

Put the object you need (the one on which you will call your add/remove method) in the Tag property of the button by doing

Tag ="{Binding}"

create an event handler for the click event of the button then execute the methods..

I hope I was clear. if you want i can post you the code for the event handler.... This is just what i would do, I hope it helps





Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

MMattD

ok, so now we are on the same page! I have already tried this approach using

Tag="{Binding}" //Tag comes back null in the event

and

Tag="{Binding PropertyName}" //Tag again comes back null

I don't know why!

However, this does work...(which you actually suggested in another forum thread to me)

Tag="{Binding ElementName=ListBox, Mode=OneWay}"

So now i can add new Items! However, it's still not as flexible as i would like it, since i have to create an event handler for every button in every style...Also, i would really like to know why i can't bind strait to the Property of the Buisness object within the tag!!

So thanks! Let me know if you ever figure out a better way to do this...really I would again prefer to do it all in XAML, but perhaps this just isn't possible within a DataTemplate!




Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

MMattD

the problem with the above Tag="{Binding}" and Tag="{Binding PropertyName}" coming back null because I set the DataContext on the ContentControl that was using the DataTemplate. Yeah, that was it! Does this make sense to anyone So then there was some mess up with the linkage, and now it works, since i removed that!




Re: Windows Presentation Foundation (WPF) Listbox Binding a Button to Add Items

Marlon Grech

aha...

so that's why... I was really confused... The reason for this is that {Binding} always try to get the DataContext set..Smile

good job on finding the problem...

WPF ROCKS


Regards