Select to view content in your preferred language

Extending Editor Widget

1393
9
10-04-2010 05:31 PM
IgressT
Emerging Contributor
I am trying to add custom functionality to the editor by adding buttons to the editor widget. However being new to Silverlight/ESRI API how do I create event handlers to the buttons I create and where does the code for the event handlers go... any help would be great. Thanks
0 Kudos
9 Replies
IgressT
Emerging Contributor
This is what I want to do. Add a new button to editor widget. User will then select a feature using the
select tool. User will then click the new button from which I will access the selected feature and will do some analysis on the selected feature.

This is what I could do. I used expression blend to "edit template - edit a copy - save it to a resource dictionary". I added a new button in the resource dictionary and used triggers to attach a click event to the new button. This works fine however how can I get the reference of the editor widget in the click event so that I can retrieve the selected feature. or is their any other easier or better way.
Thanks for any help.
0 Kudos
JenniferNery
Esri Regular Contributor
It sounds like you won't need to add an extra button if all the new button is doing is access the selected features returned using select tool.  What you can do instead is subscribe to the EditCompleted event of the EditorWidget's Editor so that you will not require an additional button click.

 public MainPage()
  {
   InitializeComponent();
   Editor editor = this.MyEditorWidget.DataContext as Editor;
   if (editor != null)
    editor.EditCompleted += editor_EditCompleted;
  }

  void editor_EditCompleted(object sender, Editor.EditEventArgs e)
  {
   if (e.Action == Editor.EditAction.Select)
   {
    foreach (var edit in e.Edits)
    {
     // Do what you need to do with edit.Graphic
     // (edit.Layer as FeatureLayer).SelectedGraphics also 
     // gives you a list of all selected graphics in the specific layer
    }
   }
  }


If you still wish to add buttons to the EditorWidget, you can modify the DefaultTemplate. It can be done using Expression Blend.This post is related and you might want to check it out: http://forums.arcgis.com/threads/9756-Undo-edits-of-the-EditorWidget

You can add something like this to your EditorWidgetStyle:
XAML-code:
<Button x:Name="MyButton" IsEnabled="True" Style="{StaticResource ButtonStyle}" Content="My New Button" Click="MyButton_Click" />

Code-behind:
 private void MyButton_Click(object sender, RoutedEventArgs e)
  {

  }
0 Kudos
IgressT
Emerging Contributor
Hi,
Thanks for the reply. I can follow your first approach however I still want to add a new button. As you suggested I edited the default template and added a new button.

I have attached my "EditorWidgetStyle.xaml" file with the custom button and as you can see from the file I used trigger to use the click event of the button . I have attached the class also "PavementPerformace.cs"
If I go this approach I still come back to my initial question. how  can I access the selected feature in my "PavementPerformace.cs" or can it be done this way?
Thanks for your help.
0 Kudos
JenniferNery
Esri Regular Contributor
Hi. Do you mind uploading a new zipped file? I was not able to open this attachment.

To answer your other question:
You can access selected features by doing the following:
FeatureLayer layer = this.MyMap.Layers["LayerID"]  as FeatureLayer;
if (layer !=  null)
     foreach(Graphic g in layer.SelectedGraphics)
     {
          // Do what you need to do with each selected graphic.
     }


Another way to find out if there are new selection in the layer is to subscribe to PropertyChanged event of that layer where e.PropertyName == "SelectedGraphics" (or "SelectionCount" - either one is fine). You can get look at SelectedGraphics or Graphics where graphic.Selected is true.
0 Kudos
IgressT
Emerging Contributor
Hi,
Thanks for the quick reply. I have attached the zip file again (if you look at the very end of the EditorWidgetStyle you will see my custom button.
0 Kudos
JenniferNery
Esri Regular Contributor
It seems the attached file is corrupted because of the file size. In case there's no need for me to check your EditorWidgetStyle, you can use the new button's event handler to access the selected graphics. You can try this first and see if this works for you. If not, you can try to send your attachment again. But before you submit, can you do a preview post and check if the attachment can be unzipped?  Thanks.
0 Kudos
IgressT
Emerging Contributor
Hi Thanks for the reply. Please have a look at the following code. The resource dictionary has the EditorWidgetStyle (I have removed most part of it). If you look at the end I have included a custom button and with use of trigger I am attaching a click event.

The click event is then handled in the PavementPerformace.cs included below. The code till here works with no problem. The click event is fired and is handled in the class. however as I described in my earlier posts how can I know the selected feature in the "Target_Click"

<ResourceDictionary
<Style x:Key="EditorWidgetStyle2" TargetType="esri:EditorWidget">
...
    <Grid x:Name="ToolsContainer1" 
        Background="{TemplateBinding Background}" 
        HorizontalAlignment="Center" >
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,0">
      <!--SELECT BUTTON-->    
      <Button x:Name="Select" Command="{Binding Select}" />

      <!--CUSTOM BUTTON-->  
      <Button x:Name="PavementPerformace" Height="40" Width="40">
          <i:Interaction.Triggers >
              <i:EventTrigger EventName="Click">
                  <local:PavementPerformace/>
              </i:EventTrigger>
          </i:Interaction.Triggers>                                    
      </Button>  
     </StackPanel>       
    </Grid>
</Style>                       
</ResourceDictionary>



PavementPerformace.cs
   
public class PavementPerformace:TargetedTriggerAction<Button>
    {
        protected override void Invoke(object parameter){}
        protected override void OnAttached()
        {
            base.OnAttached();
            Target.Click += new RoutedEventHandler(Target_Click);
        }
        void Target_Click(object sender, RoutedEventArgs e)
        {
            //Access selected feature and calculate pavement performance          
        }        
        protected override void OnDetaching()
        {
            base.OnDetaching();
            Target.Click -= new RoutedEventHandler(Target_Click);
        }
    }
0 Kudos
JenniferNery
Esri Regular Contributor
You will know which features were selected if you have access to the Editor or the FeatureLayer so your PavementPerformanceAction need to expose some Dependency properties for Editor or FeatureLayer.

Either of these events can give you what you need:
Editor.EditCompleted event
FeatureLayer.PropertyChanged event
0 Kudos
IgressT
Emerging Contributor
Hi,
Exposing Dependency properties did the trick.

Thanks
0 Kudos