How to combine a collection that's an object property (for collectionviewsource) - c#-4.0

I have a CollectionViewSource that gets bound to an ObservableCollection in a ViewModel, for this we'll call it
ObservableCollection<ItemCollection> Items;
Now, I have another collection which holds a Collection of Collections that gets updated and returned from a service (the service is updating the actual object, the container is static).
ObservableCollection<ItemCollectionContainer> Container;
Each "Container" has a property "Items" which has a list.
What I want to display is the complete List of ALL Container.Items put in 1 collection, and if possible, if an items gets added to Container.Items, it gets updated.
Any Ideas?
Thanks!

All I can say is I must fail in explaining what I was looking for of it was so simple, people wanted me to figure it out myself :)
Answer is
viewModel.Items = new ObservableCollection<Item>(Container.SelectMany(x => x.Items));

Related

How to use <sw-entity-multi-select> correctly?

I am a bit confused how to use the component <sw-entity-multi-select>. I understand that the difference between this component and the <sw-entity-multi-id-select> is that the first one returns the entities and the latter one returns just the id of the selected entities. But from the structure and the props they are totally different.
I am confused, because I mainly use the component as this:
<sw-entity-multi-select
entityName="language"
:entity-collection="languages"
:criteria="salesChannelLanguageCriteria"
:label="Language"
#change="selectLanguage"
>
</sw-entity-multi-select>
I could remove the entityName here, as the name is retrieved from the collection as well. But when I dig into the core, I see that inside selectLanguage I should do this:
selectLanguage(languages) {
this.languageIds = languages.getIds();
this.languages = languages;
}
I now understand that languageIds are kind of the v-model that determine, which entities should be selected in the component. Is this true? Why do I have to set the this.languages here again then? To me it's kind of magic if languageIds have this role here, because it's not referenced anywhere on the component. How does it work and how do I tell the component which items are selected - is using languageIds the correct way?
I now understand that languageIds are kind of the v-model that determine, which entities should be selected in the component. Is this true?
No. This example probably just extracts the IDs for some other use, e.g. for adding associations of language to another entity. One could arguably that if this is the only purpose of the selection sw-entity-multi-id-select might be the better component to use.
Why do I have to set the this.languages here again then?
Because you want to store the updated entity collection to persist the selection. Whatever is selected within the multi select is derived from that collection. So, let's say, initially you start out with an empty entity collection. You select some entities and the change is emitted with the updated collection containing the selected entities. Given we have :entity-collection="languages" we then want this.languages to be this updated collection, so the selection persists. So we kinda complete a loop here.
On another note, you could also use the collection with v-model="languages". In that case any additions or removals within the selection would be applied reactively to the collection and you wouldn't need to set this.languages after each change and you could also remove :entity-collection="languages". So basically, which of these approaches you use depends on whether you want your changes applied reactively or not.

ExtLib, iNotes List View: how can I access data selected in the view from the outside?

Using Firebug I found that the iNotes List View object has a function called "getSelectedData()" delivering something like an array of selected view entries (each one consisting of the item specific row data, like the "ext" element described here by Paul Withers). Thus, using one of List View's internal events (like "onContextMenu"), I can retrive selected data and put them somewhere else. Which is just great!
But, as I'm never content with what I have, now I'm looking for a way to address the List View's object from the outside (e.g. using a button) to access a selected data collection in a similar or even the same way. But no matter what I try, I can't seem to get to the proper object from outside of the List View itself. Using
dojo.byId("#{id:listView1}")
is giving me an object but without any of those specific methods that I need. Neither Google, nor openNtf or the ExtLib book itself has any info on that.
Any hint?
Greets,
Lothar
I guess I solved it. I've been close yesterday, but using dojo.byId instead of dijit.byId prevented it from working:
var grid = dijit.byId("#{id:listView1}");
var sel = grid.getSelectedData();
Result is a named array of row data where each row entry contains all the relevant view entry data for that row.
Works like a charm!
- Lothar

System.ComponentModel.BindingList: Add(object) vs. AddNew()

What is the difference between the System.ComponentModel.BindingList methods Add(object) and AddNew()? The MSDN documentation says this:
Add: Adds an object to the end of the Collection<T>.
AddNew: Adds a new item to the collection.
It seems like both methods add an item to the collection, but Add(object) does it in one shot whereas AddNew() is slightly more complicated. My tests with Add(object) seem to be working, but I want to know if I am using the correct method.
So what is the difference between these methods?
AddNew() creates the object for you (that's why it doesn't have a parameter).
It's designed to be used by grids, which don't know how to create a new object to pass to Add().
AddNew() is very handy (it’s the well-known Factory design pattern) when you implement a class derived of BindingList().
It allows your code to initialize new items with values that depend on the list itself - e.g. a foreign key to the parent object if the binding list contains a list of children.

Can't update a sharepoint item while check fields on existing list inside delegates

I had the next code inside the delegate to create fields
delegate(SPList list)
{
list.Fields.Add(...);
//I teared off the other same strings
}
I invoked this code in the next way
modifyFunction.Invoke(list);
//modifyFunction is a previous delegate, that was declared like
//delegate void ModifyList(SPList list);
So, everything was fine, when I try to update item in this list.
But I had to dynamically add fields to the list soon. So, I changed my delegate like
delegate(SPList list)
{
CheckMethod(list);
}
void CheckMethod(SPList list)
{
if (!list.Fields.ContainsField(...))
{
list.Fields.Add(...);
}
}
After this modification (there was no modification in the code anymore) while trying to update an item of this list I have the next exception
Invalid data has been used to update the list item. The field you are trying to update may be read only
Such decisions like SPWeb.AllowUnsafeUpdates or SPSecurity.RunWithElevatedPrivileges didn't give any positive results. Where is the trick? I'll appreciate any help. Thank you.
It is a little bit hard to guess what your are exactly doing by analyzing the code parts that your are providing.
But I guess that you load the item from the list
then you modifiy the list by adding a new field.
And then you update the item.
Now, the problem, I guess, is that you have still the same reference to the item after the list has changed.
To solve this problem, you have to reload the item after the modification on the list and then call update on this item.

Reflect changes to objects in a relationship in parent object with NSFetchedResultsController

I have two entities event and time. The event entity has a 1 to many relationship to time entities as each event can be performed multiple times. Now I want to display all the events chronologically in a tableView. So I set up a fetchedResultsController to fetch all time objects, sort them according to the start time and display the event information by using the relationship to the event object. So far so good. But now if the user tabs an entry in the table I pass an event object to the detailViewController where the event can be edited.
The problem is that now only the event entity is marked as updated. I found this out by looking at the userInfo directory of the NSManagedObjectDidChange notification. In consequence the delegate methods on the FRC are not fired as no time objects have been changed.
How can I manually mark a time object as changed to make the FRC recognize the changes and update the cells accordingly? I tried firing the KVO methods willChangeValueForKey and didChangeValueForKey but it did not work so far.
Thanks alot
Thomas
My model is a little different, but it can easily be translated to your one.
I got a tree-like structure:
Element
title
parent (to-one)
Folder : Element
children (to-many)
File : Element
When a file gets added or deleted, only the first folder in the queue up gets notified about this change. When a file's title changes, not a single folder would get notified. So, what to do?
I tried overriding -willChangeValueForKey: and -didChangeValueForKey: in my Element class.
- (void)willChangeValueForKey:(NSString *)key
{
[super willChangeValueForKey:key];
[self.parent willChangeValueForKey:#"children"];
}
- (void)didChangeValueForKey:(NSString *)key
{
[super didChangeValueForKey:key];
[self.parent didChangeValueForKey:#"children"];
}
Basically, what this does is forcing the parent folder to update because one of its children changed.
Hope it works for you, too.
I'm working through some similar types of updates right now as well. Here's the way I approached the problem.
Let's say we have object A, which relates to object B. B has a property C. We want changes to property C to be reflected in FRCs that use A as the fetched object. What I did to make this happen was to define an accessor to property C from object A:
//A.m
- (void)setC:(int)cValue {
[self willChangeValueForKey:#"b"];
self.b.c = cValue
[self didChangeValueForKey:#"b"];
}
- (int)c {
return self.b.c;
}
This allowed my cells to update based on FRC callbacks with type NSFetchedResultsChangeUpdate. Hopefully this helps solve your problem.
The answer above from #Jenox appears to be the right idea, but it's best to not override those methods as they're called whenever any key is changed on the child object and will probably impact performance and cause unexpected side-effects (it did for me). Probably best to just call them in whatever method you make the changes to the child object in, like this:
- (void)updateFromDictionary:(NSDictionary *)aDictionary {
[myParentModel willChangeValueForKey:#"myChildObject"];
[super updateFromDictionary:aDictionary];
[myParentModel didChangeValueForKey:#"myChildObject"];
}
Note that updateFromDictionary is one of my methods, not a system method.

Resources