Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Refresh Icon in SolutionExplorer depending on item properties

$
0
0

Hi,

I am working on a custom project flavor that has to meet the following requirement:

An item with a certain Build Action "Custom_ModelFile" assigned must have a custom (boolean) property "Include in Package". For UX reasons, these items must have a dynamic menu item in their context menu, that allows to set the custom property with one click. In order to visualize the current value of the property, the icon of the item must be set accordingly.

I've actually implemented all the features described above, but what's making me desperate is the fact, that the icon in solution explorer is not updated when the custom property is changed by the menu commands. When I reload the whole project, the correct icon is shown. 

Here's what the menu commands are doing (only the "Include" command, "Exclude" works analogue):

private void IncludeModelFile(object sender, EventArgs e){
    var selectionService = IVsMonitorSelection)this.GetService(typeof(SVsShellMonitorSelection));
    EnvDTE.ProjectItem projectItem = selectionService.GetSelectedContextObject() as EnvDTE.ProjectItem;
    if (projectItem != null)
        projectItem.SetItemMetadata("IncludeInPackage", "true");
    ((VSProject)projectItem.ContainingProject.Object).Refresh();
}

SetItemMetadata is an Extension method implemented as follows:

public static void SetItemMetadata( this EnvDTE.ProjectItem projectItem, string propertyName, object value )
{
        IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
        IVsHierarchy hierarchy;
        solution.GetProjectOfUniqueName(projectItem.ContainingProject.UniqueName, out hierarchy);
        IVsBuildPropertyStorage buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;
	if (buildPropertyStorage != null)
	{
		string fullPath = (string)projectItem.Properties.Item("FullPath").Value;
		uint itemid;
		hierarchy.ParseCanonicalName(fullPath, out itemid);
		buildPropertyStorage.SetItemAttribute(itemid, propertyName, value.ToString());
	}
}

As mentioned above, the custom property is set (persisted as item metadata in csproj file), but the solution explorer updates the icon not before I reload the project OR notbefore I rename the file.

I used AdviseHierarchyEvents method to hook in hierarchy events in order to get a deeper understanding of what's going on when I change the custom property. I found out that theOnPropertyChanged(uint itemid, int propid, uint flags) method of my HierarchyEvents class gets called when the custom property is changed. One call is raised with argument 'propid' set to __VSHPROPID.VSHPROPID_IconIndex.My understanding is, that the IVsHierarchy indeed notifies the outer environment of a changed icon index. But the outer environment - in this case the Solution Explorer - ignores the notification. 

When i rename the file, event handlers OnItemDeleted and OnItemAdded are called successively and the Solution Explorer refreshes the icon. 

Does anybody have a solution for this problem? How is one able to force a complete refresh of the Solution Explorer UI?

Thank you for your time,
Joerg 





Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>