C# – Using InfoWindow with Feature Layer and DialogWindowBase

arcgis-api-wpfcinfowindowmapcontrolwpf

I need to have an infoWindow that displays the Outfields of a FeatureLayer. The infoWindow is popping up, but it is popping up no matter where you click on the map and it does not contain any of the content. I have looked through the documentation and the only difference is that instead of using a UserControl, I am using a DialogWindowBase. Below is my XAML and C#.

XAML:

<DataTemplate x:Key="MyFeatureLayerInfoWindowTemplate">
                    <StackPanel Margin="7">
                        <TextBlock Text="{Binding [WVAL_ID], StringFormat='ID: \{0\}'}" FontWeight="Bold" Foreground="White"  />
                        <TextBlock Text="{Binding [SIZE], StringFormat='Size: \{0\}'}" FontWeight="Bold" Foreground="White" />
                        <TextBlock Text="{Binding [WVAL_USE], StringFormat='Use: \{0\}'}" FontWeight="Bold" Foreground="White" />
                        <TextBlock Text="{Binding [WVAL_TYPE], StringFormat='Type: \{0\}'}" FontWeight="Bold" Foreground="White"  />
                        <TextBlock Text="{Binding [OPENS], StringFormat='Opens: \{0\}'}" FontWeight="Bold" Foreground="White"  />
                        <TextBlock Text="{Binding [TURNS], StringFormat='Turns: \{0\}'}" FontWeight="Bold" Foreground="White"  />
                        <StackPanel Orientation="Horizontal" >

                        </StackPanel>
                    </StackPanel>
                </DataTemplate>

<esri:Map x:Name="MyMap" Background="#FFE3E3E3" WrapAround="True" MouseClick="MyMap_MouseClick" MouseRightButtonDown="MyMap_MouseRightButtonDown" Extent="-9834972.92753924,4441899.425293319,-9833977.88119163, 4442762.485358352">
               <esri:Map.Layers>

                    <esri:ArcGISTiledMapServiceLayer ID="MyLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
                    <esri:ArcGISDynamicMapServiceLayer ID="Calvert_City" Url="http://localhost:6080/arcgis/rest/services/CalvertCity_Test_2/MapServer"/> 

<esri:FeatureLayer ID="Water_Valves" Url="http://localhost:6080/arcgis/rest/services/CalvertCity_Test_2/MapServer/0" Renderer="{StaticResource MySimpleRenderer}" OutFields="WVAL_ID,SIZE,WVAL_USE,WVAL_TYPE,OPENS,TURNS" MouseLeftButtonDown="FeatureLayer_MouseLeftButtonDown">
    </esri:FeatureLayer>
                    </esri:Map.Layers>
      </esri:Map>

            <esri:InfoWindow x:Name="MyInfoWindow"
                             Padding="2"
                             CornerRadius="20"
                             Background="{StaticResource PanelGradient}"
                             Map="{Binding ElementName=My_Map}"
                             ContentTemplate="{StaticResource MyFeatureLayerInfoWindowTemplate}"
                         MouseLeftButtonUp="MyInfoWindow_MouseLeftButtonUp">

            </esri:InfoWindow>

C#:
Here is the C# code that I have that will at least build and allow the infoWindow to appear:

private void MyMap_MouseClick(object sender, Map.MouseEventArgs e)
        {

            FeatureLayer featurelayer = MyMap.Layers["Water_Valves"] as FeatureLayer;
            System.Windows.Point screenPoint = MyMap.MapToScreen(e.MapPoint);

            MyInfoWindow.Anchor = e.MapPoint;
            MyInfoWindow.IsOpen = true;
        }

Here is the way the documentation does it:

 private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            FeatureLayer featureLayer = MyMap.Layers["MyFeatureLayer"] as FeatureLayer;
            System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);

            // Account for difference between Map and application origin
            GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.MainWindow);
            System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);

            IEnumerable<Graphic> selected =
                featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);

            foreach (Graphic g in selected)
            {

                MyInfoWindow.Anchor = e.MapPoint;
                MyInfoWindow.IsOpen = true;
                //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate
                MyInfoWindow.Content = g.Attributes;
                return;
            }
        }

When I try to run it while using the generalTransform is says that is cannot do it because it is not an ancestor.

Best Answer

I have a feeling that the original did not work because the map name does not match the infowindow binding:

<esri:Map x:Name="MyMap">

Map="{Binding ElementName=My_Map}"

Related Question