Select to view content in your preferred language

Find add in - problems with mouse click event

739
5
Jump to solution
05-24-2012 07:13 AM
KarenEllett
Regular Contributor
I'm attempting to write an addin that will allow me to find specific features based on a particular attribute value. (In this case, pole number) When I attempt to run the program, I get the error "Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'." I have the event handler in the code, so I'm not quite sure what the problem is. Any help?

My xaml:

<UserControl x:Class="FindTool.AddIns.MyConfigDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
    xmlns:slData="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d: DesignHeight="400" d: DesignWidth="600">
   
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <esri:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" Size="8" Color="Red" Style="Circle" />
            <esri:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Red" Width="6"  />
            <esri:SimpleFillSymbol x:Key="DefaultFillSymbol" BorderBrush="Red" BorderThickness="2" Fill="#50FF0000"/>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="150" />
        </Grid.RowDefinitions>
      

        <Border BorderBrush="Black" CornerRadius="5" BorderThickness="1" Height="35" MinWidth="250" VerticalAlignment="Top"
                Background="#77919191" Margin="24,10,0,0" HorizontalAlignment="Center" Width="388">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="1" />
            </Border.Effect>
            <Grid MinWidth="250" Width="332">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="Search for" Foreground="White" Grid.Column="0"
                           HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
                           FontWeight="Bold" FontSize="12" Margin="20,8,5,0"/>
                <TextBox x:Name="FindText" Background="White" Text="Pole Number" Height="23" Width="100" HorizontalContentAlignment="Center" Grid.Column="1" />
                <TextBlock Text=":" Foreground="White"  Grid.Column="2"
                           HorizontalAlignment="Center" Height="24" VerticalAlignment="Center"
                           FontWeight="Bold" FontSize="12" Margin="5,8,5,0"/>
                <Button x:Name="ExecuteButton" Content="Find" Width="75" Height="24" VerticalAlignment="Center"  Click="ExecuteButton_Click"
                        Margin="5,0,5,0" Cursor="Hand"  Grid.Column="3" />
            </Grid>
        </Border>

        <slData: DataGrid x:Name="FindDetailsDataGrid" AutoGenerateColumns="False" HeadersVisibility="All" Background="White"
                         BorderBrush="Black" BorderThickness="1" SelectionChanged="FindDetails_SelectionChanged"
                         HorizontalScrollBarVisibility="Hidden" IsReadOnly="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Margin="0,51,0,60">
            <slData: DataGrid.Columns>
                <slData: DataGridTextColumn Binding="{Binding Path=LayerId}" Header="Layer ID" />
                <slData: DataGridTextColumn Binding="{Binding Path=LayerName}" Header="Layer Name"/>
                <slData: DataGridTextColumn Binding="{Binding Path=FoundFieldName}" Header="Found Field Name" />
                <slData: DataGridTextColumn Binding="{Binding Path=Value}" Header="Found Field Value"/>
            </slData: DataGrid.Columns>
        </slData: DataGrid>
    </Grid>
</UserControl>



C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.ComponentModel;
using System.Security;
using System.Runtime.InteropServices;
using System.Diagnostics;
using ESRI.ArcGIS.Client.Extensibility;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Tasks;

namespace FindTool.AddIns
{
    public partial class MyConfigDialog : UserControl
    {
        public MyConfigDialog()
        {
            InitializeComponent();
         
       }



        private void ExecuteButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
          
         
            GraphicsLayer graphicsLayer = MapApplication.Current.Map.Layers ["MyGraphicsLayer"] as GraphicsLayer;
            graphicsLayer.ClearGraphics();

            FindTask findTask = new FindTask("http://gisadv02dv/ArcGIS/rest/services/Electric_Distribution_Web/Elec_Dist/MapServer/25");
            findTask.Failed += FindTask_Failed;

            FindParameters findParameters = new FindParameters();
            // Layer ids to search
            findParameters.LayerIds.AddRange(new int[] {25});
            // Fields in layers to search
            findParameters.SearchFields.AddRange(new string[] { "POLENUMBER" });
            // Return features in map's spatial reference
            findParameters.SpatialReference = MapApplication.Current.Map.SpatialReference;

            // Bind data grid to find results.  Bind to the LastResult property which returns a list
            // of FindResult instances.  When LastResult is updated, the ItemsSource property on the
            // will update. 
            Binding resultFeaturesBinding = new Binding("LastResult");
            resultFeaturesBinding.Source = findTask;
            FindDetailsDataGrid.SetBinding(DataGrid.ItemsSourceProperty, resultFeaturesBinding);
           
            findParameters.SearchText = FindText.Text;
            findTask.ExecuteAsync(findParameters);

            // Since binding to DataGrid, handling the ExecuteComplete event is not necessary.
        }

        private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Highlight the graphic feature associated with the selected row
            DataGrid dataGrid = sender as DataGrid;

            int selectedIndex = dataGrid.SelectedIndex;
            if (selectedIndex > -1)
            {              
                FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;
                Graphic graphic = findResult.Feature;

                switch (graphic.Attributes["Shape"].ToString())
                {
                  case "Polygon":
                    graphic.Symbol = LayoutRoot.Resources["DefaultFillSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                    break;
                  case "Polyline":
                    graphic.Symbol = LayoutRoot.Resources["DefaultLineSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                    break;
                  case "Point":
                    graphic.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.Client.Symbols.Symbol;
                    break;
                }

                GraphicsLayer graphicsLayer = MapApplication.Current.Map.Layers["MyGraphicsLayer"] as GraphicsLayer;
                graphicsLayer.ClearGraphics();
                graphicsLayer.Graphics.Add(graphic);
            }
        }

        private void FindTask_Failed(object sender, TaskFailedEventArgs args)
        {
            MessageBox.Show("Find failed: " + args.Error);
        }

       
    }
}
0 Kudos
1 Solution

Accepted Solutions
KarenEllett
Regular Contributor
Figured this problem out!

View solution in original post

0 Kudos
5 Replies
KarenEllett
Regular Contributor
I've actually solved the first issue; now my tool deploys when I click on it in the viewer.  I get a window asking me to enter a pole number, but when I click the "Find" button, I get a new error, "Object reference not set to an instance of an object." 
The majority of the code shown above is still the same, the only changes are to define the grid rows.  Any help?
0 Kudos
KarenEllett
Regular Contributor
Figured this problem out!
0 Kudos
ScottBailey
Deactivated User
Are you willing to post the addin when you have it working. I would also like that functionality
0 Kudos
KarenEllett
Regular Contributor
Sure; I'm making a few more changes to it, then I'll post it.  I'll let you know when it's up.
0 Kudos
EthanSnyder
Emerging Contributor
Does anyone know where I can get this Find add-in? Thanks
0 Kudos