Select to view content in your preferred language

Attachment Editor integratedwith the Toolkit Editor Widget Select

1296
7
09-12-2011 02:54 PM
DavidAshton
Frequent Contributor
I took Toolkit Editor Widget SDK example code and added a new button to the Toolbar that opens and closes the Attachment Editor dialog/grid/collapsible window. 

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ToolkitAttachmentEditor
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#ToolkitEditorWidget

But I don't want to use the  MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp" to select a feature and activate the attachment code ("add" button)

 <esri:FeatureLayer ID="IncidentsLayer"
                               Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"
                               MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"
                               AutoSave="False"                  
                               DisableClientCaching="True" 
                               OutFields="*"
                               Mode="OnDemand" />


 
private void FeatureLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
        {
            FeatureLayer featureLayer = sender as FeatureLayer;

            for (int i = 0; i < featureLayer.SelectionCount; i++)
                featureLayer.SelectedGraphics.ToList().UnSelect();

            e.Graphic.Select();
            MyAttachmentEditor.GraphicSource = e.Graphic;
        }


I would like to have the attachment feature (the add button) become available when a feature is selected using the "New Selection" tool from the ToolkitEditorWidget.  I know I might run into problems if multiple feature are selected but I figured I could always write something late to check that (hopefully lol). 

Is this even possible?  I've been trying for some time now and have had little to no luck so I was wondering if someone could push me along.

Thanks
Dave
0 Kudos
7 Replies
JenniferNery
Esri Regular Contributor
You can remove FeatureLayer.MouseLeftButtonUp and use Editor.EditCompleted event instead.

If you added the attachment button in the EditorWidget Style, you can add the following to the attachment button properties to activate Editor.Select with it. Also add a Button.Click event to indicate when attachment button is clicked so your EditCompleted event handler can know when to enable AttachmentEditor.

Command="{Binding Select}" CommandParameter="new" Click="Button_Click"


bool attachmentEditorActive = false;

private void Button_Click(object sender, RoutedEventArgs e)
{
 attachmentEditorActive = !attachmentEditorActive;   
}

private void EditorWidget_EditCompleted(object sender, Editor.EditEventArgs e)
{
 if (e.Action == Editor.EditAction.Select && attachmentEditorActive)
 {
  foreach (var edit in e.Edits)
  {
   MyAttachmentEditor.GraphicSource = edit.Graphic;
   break;
  }
 }
}
0 Kudos
DavidAshton
Frequent Contributor
Jennifer thanks for the reply:

so in the xaml when defining the FeatureLayer...should I put add some code in place of MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"

Seems like I need something like EndSaveEdits="EditorWidget_EditCompleted"

???=EditorWidget_EditCompleted

<esri:FeatureLayer ID="IncidentsLayer"
                               Url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"
                             //  MouseLeftButtonUp="FeatureLayer_MouseLeftButtonUp"

                               AutoSave="False"                 
                               DisableClientCaching="True"
                               OutFields="*"
                               Mode="OnDemand" />

Thanks for the help.
Dave
0 Kudos
JenniferNery
Esri Regular Contributor
Sorry, I missed to include that code. EditorWidget has an event called EditCompleted, you want to subscribe to that and wire to your event handler: EditorWidget_EditCompleted.
0 Kudos
DavidAshton
Frequent Contributor
O.K. Jen, thanks for your reply.  I step away for awhile and now I have a few more questions...hopefully you are still out there and can help me out.

For a review here's what I have:

In my EditorWidgetStyle xaml I have added this button

<Button x:Name="ViewAttribute" Command="{Binding Select}" CommandParameter="new" Style="{StaticResource ButtonStyle}" ToolTipService.ToolTip="View Attachments" Click="ViewAttribute_Click">
<Image Source="Images/AttImage.png" Stretch="None"/>
</Button>


When I add my feature layer here's what I define:

 <esri:FeatureLayer ID="TPOINTS" DisableClientCaching="True" 
                               AutoSave="False"
                               Url="http://myserver.com/ArcGIS/rest/services/edittest/FeatureServer/0" 
                               OutFields="*"
                               Mode="OnDemand" />



Then per your suggestion I added the necessary EditorWidget I subscribed/wired the event handler you wrote: EditorWidget_EditCompleted

 <!-- Editing Tools -->

        <userControls:CollapsiblePanel x:Name="EditorTool"  IsExpanded="True" 
                                           RenderTransformOrigin="1,0"
                                           VerticalContentAlignment="Top" HorizontalContentAlignment="Right" Margin="0,75,10,0" Effect="{StaticResource dropShadow}" >
            <Grid x:Name="EditorToolStripGrid"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >
                <Border Background="{StaticResource CommonPanelBorderBackgroundBrush}" BorderBrush="{StaticResource CommonBorderBrush}" BorderThickness="1" CornerRadius="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Border Effect="{StaticResource miniDropShadow}" Style="{StaticResource RibbonElementBorder}" Padding="15" Margin="10" >


                        <StackPanel  x:Name="EditorToolStrip" Margin="0,5,5,0" >
            <Border Background="#00919191" CornerRadius="5"
               HorizontalAlignment="Right"  VerticalAlignment="Top"
               Padding="5" BorderBrush="Transparent">
                <Border.Effect>
                    <DropShadowEffect Color="Black" Direction="-45" BlurRadius="20" Opacity=".75" />
                </Border.Effect>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Right" Margin="0,5,5,0" VerticalAlignment="Top" >
                    <esri:EditorWidget x:Name="MyEditorWidget"
                             Map="{Binding ElementName=Map}" 
                                      Width="300" 
                                      AutoSelect="False"
                                      AlwaysDisplayDefaultTemplates="True" 
                                      GeometryServiceUrl="http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"
                                      ShowAttributesOnAdd="True"  
                                      Loaded="EditorWidget_Loaded" Style="{StaticResource EditorWidgetStyle1}"
                                       EditCompleted="EditorWidget_EditCompleted"/>
                </StackPanel>
            </Border>
        </StackPanel>

                    </Border>
                </Border>
                <Image Source="images/CloseX.png" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="3" Stretch="None" Cursor="Hand" ToolTipService.ToolTip="Close" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonDown" >
                            <actions:ToggleCollapseAction TargetName="EditorTool" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Image>
            </Grid>
        </userControls:CollapsiblePanel>
        
        <!-- END Editing Tools-->

        
        <!-- Attachment Tools -->


        <Grid x:Name="AttachmentGrid"  HorizontalAlignment="Right"  VerticalAlignment="Center" Margin="0,20,15,0" Visibility="Collapsed">
            <Border Background="{StaticResource CommonPanelBorderBackgroundBrush}" BorderBrush="{StaticResource CommonBorderBrush}" BorderThickness="1" CornerRadius="6" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Border Effect="{StaticResource miniDropShadow}" Style="{StaticResource RibbonElementBorder}" Padding="15" Margin="10" >


                    <StackPanel Orientation="Vertical">
                            <TextBlock Text="Click on a point feature to select it, and click the 'Add' button to attach a file." 
                       Width="275" TextAlignment="Left" Margin="20,20,20,5" TextWrapping="Wrap" FontWeight="Bold"/>
                            <esri:AttachmentEditor x:Name="MyAttachmentEditor" VerticalAlignment="Top" Margin="20,5,20,20" 
                             Background="WhiteSmoke" Width="280" Height="190" HorizontalAlignment="Right"                              
                             FeatureLayer="{Binding Layers[TPOINTS], ElementName=Map}" 
                             Filter="All Files (*.*)|*.*|Image Files|*.tif;*.jpg;*.gif;*.png;*.bmp|Text Files (.txt)|*.txt" 
                             FilterIndex="1" Multiselect="True"
                             UploadFailed="MyAttachmentEditor_UploadFailed">
                            </esri:AttachmentEditor>
                        </StackPanel>

                    </Border>
                </Border>
                <Image Source="images/CloseX.png" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="3" Stretch="None" Cursor="Hand" ToolTipService.ToolTip="Close" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseLeftButtonDown" >
                            <actions:ToggleCollapseAction TargetName="AttachmentTool" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Image>
            </Grid>


        <!-- END Attachment Tools-->



In my code behind C# code I have


    #region Edit Tools

        private void EditorWidget_Loaded(object sender, RoutedEventArgs e)
        {
            string[] myLayerIDs = { "TPOINTS" };
            MyEditorWidget.LayerIDs = myLayerIDs;
        }

        #endregion 



        #region Attachment Tool

        private void MyAttachmentEditor_UploadFailed(object sender, ESRI.ArcGIS.Client.Toolkit.AttachmentEditor.UploadFailedEventArgs e)
        {
            MessageBox.Show(e.Result.Message);
        }


        private void ViewAttribute_Click(object sender, RoutedEventArgs e)
        {
            if (AttachmentGrid.Visibility == Visibility.Visible)
            {
                AttachmentGrid.Visibility = Visibility.Collapsed;
               attachmentEditorActive = false;
            }

            else
            {
                AttachmentGrid.Visibility = Visibility.Visible;
             attachmentEditorActive = !attachmentEditorActive;   

            }
        }

        //public event EventHandler<Editor.EditEventArgs> EditCompleted
        //{

        //}

        private void EditorWidget_EditCompleted(object sender, Editor.EditEventArgs e)
        {
            if (e.Action == Editor.EditAction.Select && attachmentEditorActive)
            {
                foreach (var edit in e.Edits)
                {
                    MyAttachmentEditor.GraphicSource = edit.Graphic;
                    break;
                }
            }
        }


        #endregion 



I have a few quick questions, i've been working on and can't seem to figure out:

1. when I try to attach something I get an the error "Upload Failed"  Fiddler states something like - {"addAttachmentResult":{"objectId":-1,"globalId" : null,"success":false,"error":{"code":0,"description":""}}}

When I check the rest endpoint I see
Has Attachments: True
But I don't see any relationship: like
Relationships:
    ServiceRequest_IncidentPriority (1) -- Related To Incident Priority (1)

Should I see the relationship to the attachment tables?  If so do I need to do something special.


Question 2. If I create a new point I can't seem to get the attachment (windows explore) to launch.  The add button is active.  Should I be able to add a new feature and then add a attachment?

Question 2b.  If I create a new point and some attributes, hit the save button.  The next time I launch the application I don't see the point I created but I know I can edit the layer because I've edited with the Windows ESRI ArcGIS Phone Application.  I'm I missing something in my code to post the edits. 

Question 3.  If I use the Unselect Tool it doesn't disable the "add attachment button" is there away to hook the unselect button to the attachmentEditorActive?  Also on a side note if I have a point selected and then show the attachment editor it doesn't activate the add button I must unselect and the reselect once the attribute editor window is open. 

Thanks for everything.
Dave
0 Kudos
JenniferNery
Esri Regular Contributor
You should be getting the following response when AttachmentEditor.GraphicSource is set:
{"attachmentInfos":[{"id":4069,"contentType":"image/jpeg","size":200282,"name":"Ranger-3S.JPG"},{"id":4070,"contentType":"image/jpeg","size":264862,"name":"Ranger-4S.JPG"}]}

After adding a file attachment, you should get something like this:
{"addAttachmentResult":{"objectId":4469,"globalId" : null,"success":true}}

"objectId"=-1 seem incorrect. I wonder if it's because AutoSave is false and the server has not yet assigned an id to the graphic so the query for attachment failed. If that is the case, you need to set AttachmentEditor.GraphicSource after the new graphic is saved. In the FeatureLayer, wire up to SaveEditsFailed and EndSaveEdits to see if your edits were saved.

If the AttachmentEditor.Add button is disabled, it is possible that GraphicSource has not been set. For example, if you want to associate the AttachmentEditor after an Add or set back to null after ClearSelection, then you would need to update your condition in the EditCompleted event.

You can try the following:
if (e.Action == Editor.EditAction.ClearSelection || e.Action == Editor.EditAction.Select)
{
 MyAttachmentEditor.GraphicSource = null;
}
else if (e.Action == Editor.EditAction.Add || e.Action == Editor.EditAction.Select)
{
 foreach (var edit in e.Edits)
 {
  if (e.Action == Editor.EditAction.Add  || edit.Graphic.Selected)
  {
   MyAttachmentEditor.GraphicSource = edit.Graphic;
   break;
  }
 }
}
0 Kudos
DavidAshton
Frequent Contributor
Jennifer,

Thanks for all the post and the time.   I haven't tried you code yet because I'm thinking I have a different/serious issue and I'm hoping you might be able to shed some light on it.

I can edit the layer(s) in the MXD but I can't edit the layer with ArcGIS.com.  It allows me to sketch a new line but then it disappears and doesn't keep.  Would you mind taking a look at this web map and trying it yourself?

http://www.arcgis.com/home/webmap/viewer.html?webmap=4ca489d6460b4f1a9511887a11408f93


Any Ideas?

Thanks
David
0 Kudos
DavidAshton
Frequent Contributor
Thanks for all the Code and help Jennifer.  I got this up and going.

On thing I had to comment out is second statement in the if statement
"|| e.Action == Editor.EditAction.Select)"
Seems like once I drop the or e.Action == Editor.EditAction.Select it started working.


if (e.Action == Editor.EditAction.ClearSelection || e.Action == Editor.EditAction.Select)
{
 MyAttachmentEditor.GraphicSource = null;
}
else if (e.Action == Editor.EditAction.Add || e.Action == Editor.EditAction.Select)
{
 foreach (var edit in e.Edits)
 {
  if (e.Action == Editor.EditAction.Add  || edit.Graphic.Selected)
  {
   MyAttachmentEditor.GraphicSource = edit.Graphic;
   break;
  }
 }
}


I guess one more question is I have is what if a user has something selected but goes to select something else.  So she/he clicks and doesn't click on a point and it deselect the first selected point and now nothing is selected.  The tool is still active and the user can still add a attachment to the previous selected item.  IS there a way to check for this and make sure to set the MyAttachmentEditor.GraphicSource = null; if this happens
0 Kudos