Binding Gtk# NodeView to a IList? - c#

I've got a data object with a component in it that is an System.Collections.Generic.IList, and I'd like to reflect changes to that list into a Gtk# NodeView, so that when an item is added to the list, the NodeView will get a new item added to it.
How would I listen for changes to an IList? I have considered wrapping the IList with a class that implements IList, delegates the requisite methods, and broadcasts an event when changing it's contents, but that seems like a lot of work for something that has probably already been solved by someone else.

Gtk.DataBindings is wahat you're looking for.

Do System.Componen.BindingList or System.Collections.ObjectModel.ObservableCollection exist in mono?

Related

How do I back up the observable collection used by a WPF datagrid?

I would like to back up the observable collection used by a WPF datagrid for the purposes of a rollback/undo button to the point of the last commit. I have a refresh option that will read from the database but that will include changes from other sources. There are other similar instances of this question around, but nothing that stood out as a solution.
I have tried copying the observable collection to a list, but any changes made to the OC are also persisted to that list. But aside from a single simple assignment statement that list is not in any other way connected to the observable collection.
Its almost as if the address of the observable collection was assigned to be the address of the backup list in the manner of how it is behaving. How do I overcome this?
Converting an ObservableCollection<T> to List<T> simply changes the type of collection that holds your items. The elements inside the collection remain unchanged. What you're looking to do is cloning your list. To do this your element class will have to implement the ICloneable interface. See how to implement the IClonable interface here. Once that's in place, all you have to do is call:
var clone = myObservableCollection.Select(i => (MyType)i.Clone()).ToList();

Get selected items in solution with native interfaces

I am trying to get all selected items in a solution and this with native code. With native code I am referring to code which does not use the DTE.
I checked the documentation and tried to find a suitable solution, however I din't come very far. What I found was the IVsUiHierarchy which contains the ExecCommand method which contains the following.
Commands that act on a specific item within the hierarchy. (If ItemID equals VSITEMID_SELECTION, the command is applied to the selected item or items.)
So I suspect the method they are talking about, is the before mentioned ExecCommand one. For one I am not quite sure how I will get to the IVsHierarchy object from a IVsHierarchy or similar, on the other hand I am not really sure on how to use the ExecCommand method properly. Additionally I am not even quite certain, if this is even the 'right way' of approaching this.
Note: I am searching for a solution which does not contain the following code in this answer.
You can use IVsMonitorSelection.GetCurrentSelection, to identify all the selected items in the solution explorer.
The above will return you an IVsMultItemSelect interface which you can use to invoke IVsMultiItemSelect.GetSelectedItems to retrieve an array of VSITEMSELECTION values.
There are a few extensibilty samples, that utilize GetSelectedItems, you can use as a reference.
Sincerely,
Ed Dore

Update Single Item in the ObservableCollection without LINQ

I am trying to create a list of the Components running on the network. I am trying to get all the components in the ObservableCollection. ObservableCollection<ClsComponent> Now my question is if one of the component in the collection get changed / modified how would I be able to get it reflected to my ObservableCollection of Component
Is there a way to change the it directly in the collection itself?
What is the fast and efficient way doing it?
I have tried: to change it using the LINQ : Find the Component in the collection and change it?
var CompFound = Components.FirstOrDefault(x=>x.Id == myId);
Components.Remove(CompFound);
Components.Add(UpdatedComp);
I am very sure there should have been more optimized way of doing this. Please suggest.
Edit
I am trying to write the code in the function where I can get the parameters of Source Component and Destination Component. Function looks like this
public void UpdateComponent(ClsComponent SourceComp, ClsComponent DestComp)
{
//do something here
}
After the execution of the function I want to Replace Source Component with Destination Component.
I believe this might work for you. I am sure you might be looking for this
Components.Insert(Components.IndexOf(SourceComp), DestComp);
Components.Remove(SourceComp);
One of the most efficient way would be to use a dictionary. There are implementations of ObservableDictionary which will give you the Observable behavior while allowing a fast key-based access to the object.
Check this stackoverflow question. It includes Microsoft's
It should work like ObservableCollection, except it's also a dictionary. So you can Create ObservableDictionary<Int,ClsComponent>
To replace the value simply call
Components[myId] = destComp

Passing an object to a collection editor

I'm trying to build a check list for a ToolStripMenuItem that automatically handles the checking and unchecking of an item and then I provide an event to the programmer allowing them to handle what happens next. If something like this already exists, I would LOVE to know where it is. I've created the collection editor for my custom ToolStripMenuItem and I can add check lists to this collection of checklists. My problem is you create the collection editor like this:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor(typeof(ToolStripItemExtCollectionEditor), typeof(UITypeEditor))]
I need to be able to pass this ToolStripMenuItem's DropDownitems to this collection editor so when you add a new checklist and click on the items property of the checklist you can add/remove any one of the known ToolStripMenuItems to/from the checklist. Passing a reference won't work since all of this is happening inside an attribute and I wouldn't know where to begin if the answer is reflection.
This answer applies to VB.NET. I plan on turning this into C# for a DLL, but for now it's in vb.net because that's where I started this idea from and the language the project is in.
Here's what I have so far:
ToolStripMenuItemExt
Purpose: My custom ToolStripMenuItem.
ToolStripMenuItemExt has a CheckListSheet which contains a reference to ToolStripMenuItemExt's DropDownItems (I passed in dropdownitems byref and not byval). It has one property that returns the CheckLists object in CheckListSheet.
CheckListSheet
Purpose: Maintains a reference to the collection I'm observing through an observable collection type and an object of the collection I return in ToolStripMenuItemExt.
CheckListSheet has the CheckLists object. The dropdownitems I pass in byref are stored in an ObservableToolStripItemCollection which hopefully when I get to testing it allows me to update the collection of checklists easier since it inherits ObservableCollection(of ToolStripItemCollection). This class also has a shared function that returns the observable collection which has a scope identifier of private shared.
CheckLists
Purpose: The CollectionBase type that stores CheckList objects.
CheckList
Purpose: Stores the ToolStripItemCollection whose objects act as a single item checked checklist (only one item is checked at a time).
This has some properties for the designer and the collection for the check list. Eventually I'll add in the logic to check and automatically uncheck and raise an event for it.
MenuItemCheckListCollectionEditor
Purpose: Allows a collection of known and instantiated ToolStripItem objects to be displayed and added to a CheckList.
Right now it demands I give it a Type or array of Types so it can establish itself what type of CollectionEditor it is. I haven't be able to show a drop down of types or a drop down of ToolStripItem objects. Any class having ToolStrip in their name inherits ToolStripItem which is why I use this type of object.
If ANYONE has any advice on my current answer or can forecast any foreseeable pitfalls please share. I don't care if you talk in c# or vb.net. Maybe I just need to stop and turn this into c# code. Maybe this is impossible. I am making progress though. What would be extremely helpful is figuring out how MenuStrip's collection editor is able to populate a dropdown of ToolStripItems
[Update]
A collection Editor requires you to provide a type for it to display. This type has to inherit CollectionBase which means at design time there's no way for it to reference the dropdownitems. :sigh:

List of items implementing INotifyPropertyChanged

What is the best way to implement collection of items, where:
each item could be of different type
each item will raise PropertyChanged event on its change (parent class implements INotifyPropertyChanged)
Update:
I'm thinking on something like this:
Collection:
["Name", string:"John Doe"]
["Age", int:"32"]
["Profiles", List<Profiles>:"list of profiles"]
I will be able to add new item like this:
Collection.Add("NewItem", value);
And then change it:
Collection["NewItem"] = newValue;
Which will trigger event:
NotifyPropertyChanged("NewItem");
So, as a result, I will be able to subscribe to every-single item from that collection and have different event handlers.
Thanks.
What you need from your description is what I (and others) would call an ObservableDictionary, which Google will give you several suggestions for implementing. There's no such class built in to .NET, but there are tuturials and third-party implementations a-plenty.
The reasons I say this:
the correct interface for notifying that the content of a collection has changed is INotifyCollectionChanged, not INotifyPropertyChanged. When you add an item or call Collection["NewItem"] = newValue, the event you should fire is CollectionChanged, because it's not a property of the list that has changed - it's the content
you're clearly intending for your list to have key/value pairs, which means you can't easily use the existing ObservableCollection class.
Separately, you say "I will be able to subscribe to every-single item from that collection," which suggests that you may want to limit the "values" in your dictionary to be INotifyPropertyChanged but, I would suggest, you should create your ObservableDictionary as a generic class, and then you can choose to have the values in the dictionary be any type you need.
If it has to be a list, use
ObservableCollection<INotifyPropertyChanged>

Categories

Resources