Select to view content in your preferred language

How to see attribute values when binding to Feature Layer

1041
3
07-27-2011 05:05 AM
JohnLove
Deactivated User
I have implemented the "Configurable Silverlight Template Viewer for ArcGIS 2.1"
(here's the link http://www.arcgis.com/home/item.html?id=67f62c5c30cc4b3ca942068e404c442c ) and I need to change the background color of a bound Label in the map tip ( I changed the TextBlock to a label). I understand the IValueConverter pretty well but I'm not sure how to read an attribute value from the binding in order to determine the color to use.
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
The datacontext of the MapTip is set to the 'Attributes' of the current graphic, Attributes being a Dictionary.
So a binding such as <TextBlock Text={Binding [Label]} /> is working by supposing you have an attribute called 'Label' that you want to display.

If you have an attribute storing a Brush (let's call it BackgroundBrush), you can directly set the background color by:
<TextBlock Text={Binding Label} Background={Binding [BackgroundBrush]}/>

If your attribute is just a color or a string representing a color, you need to write a value converter to convert the color to a brush:
<TextBlock Text={Binding Label} Background={Binding [BackgroundColor], Converter={StaticResource myColorConverter}/>
0 Kudos
JohnLove
Deactivated User
Thank you for your reply Dominique,

The problem I'm having is due to TextBlocks being dynamically being generated with each one being assigned a different value by binding to the PropertyPath.


I cannot figure out how to test each textblock as it is created in order to set it's color based on the attribute value (i.e. if the value <= 50 the color = green else the color = red). I have written a ValueConverter that returns a brush based on the value passed into it. I just don't know how to get the value to pass into it.


Please see code below:

private PopupWindow CreateTipWindow(FeatureLayerInfo lyrInfo, List<string> outFields)

        {
            Dictionary<string, string> aliases = new Dictionary<string, string>();
            foreach (Field f in lyrInfo.Fields) { if (f.Name != lyrInfo.DisplayField) aliases.Add(f.Name, f.Alias); }

            PopupWindow tipWindow = new PopupWindow() { ShowArrow = false, ShowCloseButton = false, };
            tipWindow.Content = CreateFeatureTipContent(outFields, aliases);
            tipWindow.Background = this.myTaskbarWidget.Background;

            Binding titleBinding = new Binding() { Path = new PropertyPath(string.Format("[{0}]", lyrInfo.DisplayField)) };
            tipWindow.SetBinding(PopupWindow.TitleProperty, titleBinding);

            if (lyrInfo.Name == "Building")
                tipWindow.TitleFormat = "Installation ID" + ": {0}";
            else
                tipWindow.TitleFormat = lyrInfo.Name + ": {0}";
           
            return tipWindow;
        }

        private FrameworkElement CreateFeatureTipContent(List<string> fields, Dictionary<string, string> aliases)
        {
            StackPanel stackBox = new StackPanel() { Margin = new Thickness(4, 1, 4, 1), Orientation = Orientation.Vertical };
           

            foreach (string field in fields)
            {
                if (aliases.Keys.Contains(field))
                {

                   
       
                    TextBlock valueBlock = new TextBlock() { TextWrapping = TextWrapping.NoWrap };

                    Binding valueBinding = new Binding() { Path = new PropertyPath(string.Format("[{0}]", field)), StringFormat= aliases[field] + ": {0}" };

                    valueBlock.SetBinding(TextBlock.TextProperty, valueBinding);
                    stackBox.Children.Add(valueBlock);

                }
            }

            return stackBox;
        }

        private void MapControl_MouseClick(object sender, Map.MouseEventArgs mouseEventArgs)

        {
0 Kudos
DominiqueBroux
Esri Frequent Contributor
You can set a background binding by code.

After your code:
TextBlock valueBlock = new TextBlock() { TextWrapping = TextWrapping.NoWrap };
 
Binding valueBinding = new Binding() { Path = new PropertyPath(string.Format("[{0}]", field)), StringFormat= aliases[field] + ": {0}" };
 
valueBlock.SetBinding(TextBlock.TextProperty, valueBinding);


you can add code like:
 
Binding colorBinding = new Binding{
    Path = new PropertyPath(string.Format("[{0}]", field)),
    Converter = new MyColorConverter()
};
valueBlock.SetBinding(TextBlock.ForegroundProperty, colorBinding );
 


Your converter will get the value of the field as input value (due to binding to [<field>]).


Note : in Silverlight the TextBlock has no BackGround property, so to set the background you will need to surround the textblock with a container or use the 'Label' control.
0 Kudos