Select to view content in your preferred language

UniqueValueRenderer change polygon fill color dynamically

4255
29
07-15-2010 08:16 PM
xariaD
by
Occasional Contributor
How to change the fill color of a particular polygon at runtime?
0 Kudos
29 Replies
xariaD
by
Occasional Contributor
Yeah It does get executed, and the attribute is assigned as well, but the color does not change.

I am using ver 2.0
0 Kudos
xariaD
by
Occasional Contributor
Is there something I need to check on the server? Some setting on the publishing side?
0 Kudos
JenniferNery
Esri Regular Contributor
Hi Xaria,

I tried to replicate the issue using your code with minor tweaks. 
Changes to FeatureLayer:
Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/F..."
OutFields="symbolname."
Changes to UniqueValueRenderer:
Attribute="symbolname"
Changed Value property of each UniqueValueInfo to code values the FeatureServer expects (see FeatureLayer's Url)
      <esri:UniqueValueInfo Value="Residential - Very Low Density"
                                    Symbol="{StaticResource MyRedFillSymbol}" />
      <esri:UniqueValueInfo Value="Residential - Low Density"
                                    Symbol="{StaticResource MyYellowFillSymbol}" />
            </esri:UniqueValueRenderer.Infos>
In the code-behind (FeatureLayer_MouseLeftButtonDown)
Graphic g = e.Graphic;
g.Attributes["symbolname"] = "Residential - Very Low Density";

As soon as the symbolname attribute is changed, I see the graphic's fill color change from Yellow to Red if I clicked on a graphic that had Yellow/"Residential - Low Density".

The only difference from your code is that you were using Url="http://MyMapURL/MapServer/0 (A MapServer). I also tried using MapServer and the fill color would still change dynamically after changing the attribute, but ofcourse on page refresh or application restart, the graphic changes back to its original color because the FeatureLayer is read-only. Do you not see the color change at all?

It seems there's no problem with your code. The Attribute property of the UniqueValueRenderer must match a field in the FeatureServer and the Value property of each UniqueValueRender.Info must be a type/value that field accepts.

You can also listen to EndSaveEdits and SaveEditsFailed to see if a graphic is updated. 

Jennifer
0 Kudos
xariaD
by
Occasional Contributor
Jennifer,
Are you using WPF or Silverlight?

I do not see the color change at all. The graphic is updated,I included a Maptip and I can see the change in attribute value when I mouse over the graphic, after Mouse Click.

Could you please post your code so that I can see if it works in my application.
0 Kudos
xariaD
by
Occasional Contributor
You can also listen to EndSaveEdits and SaveEditsFailed to see if a graphic is updated.


The EndSaveEdits or SaveEditsfailed is never triggered!!!
So I added a BeginSaveEdits and that is not triggered either 😞
What is wrong here???
0 Kudos
JenniferNery
Esri Regular Contributor
I tried this sample in both WPF and SL application. I placed debug break points to the EndSaveEdits and SaveEditsFailed too. It would help to familiarize yourself with the feature layer first, it's field names and expected values. http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/F...

XAML-code:
  <UserControl.Resources>
        <esri:SimpleFillSymbol x:Key="MyYellowFillSymbol"
                                      Fill="YellowGreen"
                                      BorderBrush="Transparent"
                                      BorderThickness="2" />
        <esri:SimpleFillSymbol x:Key="MyRedFillSymbol"
                                      Fill="Red"
                                      BorderBrush="Transparent"
                                      BorderThickness="2" />
        <esri:UniqueValueRenderer x:Key="MyUniqueValueRenderer"
                                  Attribute="symbolname">
            <esri:UniqueValueRenderer.Infos>
                <esri:UniqueValueInfo Value="Residential - Very Low Density"
                                      Symbol="{StaticResource MyRedFillSymbol}" />
                <esri:UniqueValueInfo Value="Residential - Low Density"
                                      Symbol="{StaticResource MyYellowFillSymbol}" />
            </esri:UniqueValueRenderer.Infos>
        </esri:UniqueValueRenderer>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <esri:Map x:Name="MyMap">
            <esri:ArcGISTiledMapServiceLayer Url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
            <esri:FeatureLayer ID="towers"
                               Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/FeatureServer/2"
                               Where="1=1"
                               Renderer="{StaticResource MyUniqueValueRenderer}"                     
                               EndSaveEdits="FeatureLayer_EndSaveEdits"
                               SaveEditsFailed="FeatureLayer_SaveEditsFailed"
                               MouseLeftButtonDown="FeatureLayer_MouseLeftButtonDown">
                <esri:FeatureLayer.OutFields>
                    <sys:String>symbolname</sys:String>
                </esri:FeatureLayer.OutFields>
            </esri:FeatureLayer>
        </esri:Map>
</Grid>

Code-Behind:
        private void FeatureLayer_MouseLeftButtonDown(object sender, ESRI.ArcGIS.Client.GraphicMouseButtonEventArgs e)
        {
            Graphic g = e.Graphic;
            string currentValue = (string) g.Attributes["symbolname"];
            if(currentValue.Contains("Very"))
                g.Attributes["symbolname"] = "Residential - Low Density";
            else
                g.Attributes["symbolname"] = "Residential - Very Low Density";
        }

        private void FeatureLayer_EndSaveEdits(object sender, ESRI.ArcGIS.Client.Tasks.EndEditEventArgs e)
        {

        }

        private void FeatureLayer_SaveEditsFailed(object sender, ESRI.ArcGIS.Client.Tasks.TaskFailedEventArgs e)
        {

        } 


Jennifer
0 Kudos
xariaD
by
Occasional Contributor
Hi Jennifer,

I used your code, and I cannot see the feature layer at all.
:confused:
0 Kudos
JenniferNery
Esri Regular Contributor
It might be because sampleserver3 gets refreshed every night. You can try to add features of these feature types first and then use the unique value renderer and this code.
0 Kudos
xariaD
by
Occasional Contributor
I got it working, not by using UniqueValueRenderer though.
0 Kudos
IkaRasidina
Deactivated User
I got it working, not by using UniqueValueRenderer though.


Hi Xaria,

I also want to change polygon fill color dynamically, but I'm new in SL API Programming. Can you give the source code of simple example to me? coz i'm blind with this one.

Thank You.
0 Kudos