Select to view content in your preferred language

Relate Me

1136
2
Jump to solution
09-10-2012 02:23 PM
NathalieNeagle
Regular Contributor
I've had some previous help from Jennifer and Joe Hershman on how to handle and edit a related table (feature class). I have everything working with a featureDataGrid but now I want to use a featureDataForm and I thought it would be an easy switch but it is proving harder than I expected.

Here's some of my code:

XAML
 <!-- RELATED LINK RESOURCE-->
            <common:HierarchicalDataTemplate x:Key="TreeViewItemTemplate">
               <StackPanel Orientation="Horizontal" Margin="0">
                    <!--<Ellipse Fill="Transparent" Height="6" Width="6" StrokeThickness="2" Stroke="Black" Margin="0,0,10,0"/>-->
                 <TextBlock Text="APN:  " HorizontalAlignment="Left" FontSize="10" Margin="0"/>
                <TextBlock x:Name="TextBlockRelate" Text="{Binding Attributes[APN]}" HorizontalAlignment="Left" FontSize="10"/>
               
                </StackPanel>
            </common:HierarchicalDataTemplate>
            <!--END RELATED LINK RESOURCE-->


XAML
 <!-- GRID FOR RELATED (AP) -->

        <userControls:DraggableWindow x:Name="RelateResultsDisplay"  IsOpen="False" 
        VerticalAlignment="Bottom" HorizontalAlignment="Right" 
               Margin="15" Effect="{StaticResource dropShadow}" Title="RELATED WORK RESULTS TABLE" Background="Black">
              <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" >
                <Border Effect="{StaticResource miniDropShadow}" Style="{StaticResource RibbonElementBorder}" Padding="15" Margin="10">
                    <Grid x:Name="RelatedRowsGrid" HorizontalAlignment="Right"
                     VerticalAlignment="Top" MinHeight="170" Width="Auto">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="140" />
                            <ColumnDefinition MaxWidth="600" />
                        </Grid.ColumnDefinitions>
                         <StackPanel Orientation="Vertical" Margin="1,-5,1,5">
                          <TextBlock Margin="1,0,5,1" TextWrapping="Wrap" Grid.Column="0" 
                           Text="Click to see relates: " 
                           FontSize="10" FontFamily="Arial" Foreground="#FFFFFFFF" />

                           <basics:TreeView x:Name="SelectedWellsTreeView" MaxHeight="180"  Grid.Column="0"  Margin="1"
                                 ItemsSource="{Binding}" BorderBrush="Gray" BorderThickness=".5"
                  SelectedItemChanged="SelectedWellsTreeView_SelectedItemChanged" 
                                 ItemTemplate="{StaticResource TreeViewItemTemplate}"
                             />
                           </StackPanel>
                        <esriWidgets:FeatureDataGrid Grid.Column="1" Grid.Row="1" x:Name="MyFDG" Height="180" 
                                Style="{StaticResource FeatureDataGridRelateStyle1}"  />

                    </Grid>
                </Border>

            </Grid>
        </userControls:DraggableWindow>

        <!--END GRID FOR RELATED (AP) -->



CODE BEHIND
 //For FDG -- Jennifer's code
          //   l = Map.Layers["AP"] as FeatureLayer;

             l = new FeatureLayer()
            {
                ID = "MyLayer",
                Url = "http://myserver/ArcGIS/rest/services/Relate/FeatureServer/1",
                Mode = FeatureLayer.QueryMode.Snapshot,
            };

             l.OutFields.Add("*");
             l.Initialized += (s, e) =>
             {
                 if (l.InitializationFailure != null)
                     MessageBox.Show(l.InitializationFailure.Message);
             };
             l.InitializationFailed += (s, e) =>
             {
                 if (l.InitializationFailure != null)
                     MessageBox.Show(l.InitializationFailure.Message);
             };
             l.Initialized += new EventHandler<EventArgs>(l_Initialized);
             l.Initialize();
             MyFDG.GraphicsLayer = l;
             MyFeatureDataForm.FeatureLayer = l;

             queryTask = new QueryTask("http://dgpdch03/ArcGIS/rest/services/Relate/MapServer/0");
             queryTask.ExecuteCompleted += QueryTask_ExecuteCompleted;
             queryTask.ExecuteRelationshipQueryCompleted += QueryTask_ExecuteRelationshipQueryCompleted;
             queryTask.Failed += QueryTask_Failed;
            //End for relate




//CODE FROM JOE, JENNIFER, AND INTERACTIVE SDK
        void l_Initialized(object sender, EventArgs e)
        {
            var l = sender as FeatureLayer;

            l.Initialized += (s, f) =>
                                     {
                                         l.UpdateCompleted += UpdateCompleted;
                                         l.UpdateFailed += UpdateFailed;
                                         l.Update();
                                     };

        }
         

        private void UpdateFailed(object sender, TaskFailedEventArgs e)
        {
            MessageBox.Show(e.Error.Message);
        }

        private void UpdateCompleted(object sender, EventArgs e)
        {
            MessageBox.Show("WooHoo!");
        }
        FeatureLayer l;
        private void deleterowButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
             
             if (l != null && l.LayerInfo != null && l.LayerInfo.Fields != null)
             {
                 l.Graphics.Remove(MyFDG.SelectedGraphics[0]);
             }
 
        }


        private void addrowButton_Click(object sender, RoutedEventArgs e)
        {
            var g = new Graphic();
            if (l != null && l.LayerInfo != null && l.LayerInfo.Fields != null)
            {
                foreach (var f in l.LayerInfo.Fields)
                {
                    g.Attributes[f.Name] = null;
                    g.Attributes["GEN_PLAN"] = "GPk";
                    g.Attributes["APN"] = relateIDstring;
                }
            }
            else
            {
               
            }
            l.Graphics.Add(g);
        }

        private void SelectedWellsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            if (e.OldValue != null)
            {
                Graphic g = e.OldValue as Graphic;
                g.UnSelect();
                g.SetZIndex(0);
            }

            if (e.NewValue != null)
            {
                Graphic g = e.NewValue as Graphic;
                g.Select();
                g.SetZIndex(1);

                RelationshipParameter relationshipParameters = new RelationshipParameter()
                {
                                 
                    ObjectIds = new int[] { Convert.ToInt32(g.Attributes[SelectedWellsTreeView.Tag as string]) },
                    OutFields = new string[] { "*" },
                    RelationshipId = 0,
                    OutSpatialReference = Map.SpatialReference,
                    
                 };
                queryTask.ExecuteRelationshipQueryAsync(relationshipParameters);

                relateIDstring = g.Attributes["APN"].ToString();

            }
        }

        void QueryTask_ExecuteRelationshipQueryCompleted(object sender, RelationshipEventArgs e)
        {

            RelationshipResult pr = e.Result;
            if (pr.RelatedRecordsGroup.Count == 0)
            {
                l.Where = "1=0"; //remember _featureLayer is l

            }
            else
            {
                foreach (var pair in pr.RelatedRecordsGroup)
                {
                    IEnumerable<Graphic> graphics = e.Result.RelatedRecordsGroup.First().Value;



            l.Where = null; //remember _featureLayer is  l           
            int[] objectIds = graphics.Select(g => (int)g.Attributes["OBJECTID_1"]).ToArray();
            //now set the ObjectIDs on our FeatureLayer
            l.ObjectIDs = objectIds;

                }
            }
            l.Update();
            MyFDG.GraphicsLayer = l;

// MyFeatureDataForm.FeatureLayer = l; //HERE"S WHERE I THOUGHT I COULD JUST SET FEATUREDATAFORM
        }



All The code works and everything is great. I can add and delete records and see and edit all the relate information.

All I'm doing is adding a FeatureDataForm to my xaml
XAML
<Border x:Name="FeatureDataFormBorder" Visibility="Visible" 
                HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10,10,10,0" Width="300" Height="300" >
            <Border.Effect>
                <DropShadowEffect Color="Black" Direction="-45" BlurRadius="20" Opacity=".75" />
            </Border.Effect>
            <esri:FeatureDataForm x:Name="MyFeatureDataForm"   
                                         
                                         IsReadOnly="False" LabelPosition="Left" />
        </Border>


And in the CODE BEHIND I thought I could just set the source (FeatureLayer) for the FeatureDataForm in the QueryTask_ExecuteRelationshipQueryCompleted right after I set the GraphicLayer for my FeatureDataGrid...see red code above.


Shouldn't it be as simple as my thought process since everything is working for my FDG.

Thanks
Nathalie
0 Kudos
1 Solution

Accepted Solutions
NathalieNeagle
Regular Contributor
This did it..not sure if it is the best practice

  if (l != null)             {                 if (l.SelectionCount == 1)                 {                     foreach (Graphic g in l.SelectedGraphics)                     {                         MyFeatureDataForm.GraphicSource = g;                     }                 }             } 

View solution in original post

0 Kudos
2 Replies
NathalieNeagle
Regular Contributor
In my Code below (very end - in the QueryTask_ExecuteRelationshipQueryCompleted) if I set my FeatureDataForm Feature Layer and GraphicSouce the GraphicSource is not picked up because I guess My FDG is not populated with the related records yet.

I add a button to my FDG to try and Pass the selected Record/row in MyFDG to my FeatureDataForm. The code below kind of works. The first If and ForEach statement (in Red) doesn't work and that is the one I thought should work and I want to work...grab the select record/row and populate the FeatureDataForm.

The second if and forEach does work but not how I would like. It populates the FeatureDataForm but obviously if there are more than one related record in my FDG in populates the FeatureDataForm with the last record and not the selected row/record.

Can someone point me in the right direction. Thanks Nathalie.

  private void SeeRelateButton_Click(object sender, RoutedEventArgs e)
        {

            if (MyFDG.SelectedItems.Count == 1)
            {
                foreach (var g in MyFDG.SelectedItems)
                {
                    MyFeatureDataForm.GraphicSource = g as Graphic;
                }
            }


            if (l != null)
            {
                MessageBox.Show(l.SelectionCount.ToString());

                foreach (Graphic g in l)
                {
                    MyFeatureDataForm.GraphicSource = g;
                }
            }
0 Kudos
NathalieNeagle
Regular Contributor
This did it..not sure if it is the best practice

  if (l != null)             {                 if (l.SelectionCount == 1)                 {                     foreach (Graphic g in l.SelectedGraphics)                     {                         MyFeatureDataForm.GraphicSource = g;                     }                 }             } 
0 Kudos