using following to change the fill color of graphicelement:
QueuedTask.Run(() =>
{
// Access the CIM definition of the graphic element
var cimElement = graphicElement.GetDefinition() as CIMElement;
if (cimElement == null)
return;
// Get the current background symbol
var currentSymbol = graphicElement.GetGraphic().Symbol;
// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));
// Apply the modified symbol back to the GraphicElement
graphicElement.GetGraphic().Symbol = polygonSymbol;
}
// Apply the changes back to the graphic element
graphicElement.SetDefinition(cimElement);
});
No compile/runtime error, but the color does not change on UI...
Any inputs..?
Is there another way to change the color or flash/highlight a given graphicelement on a graphicslayer?
Solved! Go to Solution.
Gurunara, if this answers your question please mark this post as the solution, thanks
You are close, - almost there. Use graphicElement.SetGraphic(...) - https://pro.arcgis.com/en/pro-app/latest/sdk/api-reference/topic11077.html - to apply the changes to the graphic symbol _back_ to the element. There is a code snippet there u can use.
Do the following?
QueuedTask.Run(() =>
{
// Access the CIM definition of the graphic element
var cimElement = graphicElement.GetDefinition() as CIMElement;
if (cimElement == null)
return;
// Get the current background symbol
var currentSymbol = graphicElement.GetGraphic().Symbol;
// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));
// Apply the modified symbol back to the GraphicElement
graphicElement.GetGraphic().Symbol = polygonSymbol;
graphicElement.SetGraphic(graphicElement.GetGraphic());
}
// Apply the changes back to the graphic element
graphicElement.SetDefinition(cimElement);
});
No. This is the problem:
graphicElement.GetGraphic().Symbol = polygonSymbol;
The CIM uses a transactional pattern. You get a local copy, not a reference, of the deserialized state that u can modify (as needed) Changes must be _applied_ back via a commit.
So, get the graphic (local copy) and _hold_ a reference (to that copy).
var graphic = graphicElement.GetGraphic();
Next, apply your change to the copy whose reference you are holding:
graphic.Symbol = polySymbol [or polySymbol.MakeSymbolReference() if u hv a symbol]
Now, commit the change(s) made to the local copy _back_
graphicElement.SetGraphic(graphic);
Gurunara, if this answers your question please mark this post as the solution, thanks
Got it to work (based on feedback):
// Check if the symbol is a fill symbol
if (currentSymbol is CIMSymbolReference polygonSymbol)
{
// Change the fill color
polygonSymbol.Symbol = SymbolFactory.Instance
.ConstructPolygonSymbol(ColorFactory.Instance.CreateColor(newColor));
var graphic = graphicElement.GetGraphic();
graphic.Symbol = polygonSymbol;
// Apply the changes back to the graphic element
graphicElement.SetGraphic(graphic);
}