Select to view content in your preferred language

User controls - code structuring

1756
12
10-20-2010 11:38 AM
Ann_KristinEkeberg
Emerging Contributor
In my organization we are going to use the same user controls in multiple applications. All these user controls interact with the map. One example is a user control to display a layer list (TOC). The user control is bound to the map element. I would like to structure my code and have all these user controls in a separate project. The result is dll files that I can use in the various applications.

The challenge:
The user control has been tested and works fine when all the code is in the MainPage.xaml. When it is pulled out to a separate project I have noticed the following: The user controls in the page get loaded before the map control. (The map control is placed on top of the xaml page.) This means that the user control gets a null value for the map element on startup. If I place a refresh button inside the user control and clicks it after the map has been loaded, the user control gets the data it should.

Has anyone seen this? Any ideas how to solve it?
0 Kudos
12 Replies
DominiqueBroux
Esri Frequent Contributor
I added the code in the code behind, but I still get an error in the legenddialog.xaml indicating the ..........The property 'Map' was not found in type 'Control'

Did you get this error in the design view of VS or during the compilation?

If it's in the design view only, it's not important. You need at least one succesful compil in order the designer to know about your new property. In this case the compil should work whatever you see during the design.

Still not sure if this is correct....

At first glance, it looks good.
0 Kudos
dotMorten_esri
Esri Notable Contributor
If you were doing element binding, it won't work across user controls, since the name resolving won't go outside the scope of the user control.
You will need a Map property on your usercontrol, that can "pipe" the property forward from the MainPage xaml, through your user control and on to the controls inside that.  So the controls inside your user control binds to this property, and you set this property on your usercontol in MainPage with normal element binding.
0 Kudos
gabrielvazquez
Deactivated User
I am doing something very similiar, I'm capturing various functionality in various unique UserControls seperate to the Mainpage. Similar to below I am also declaring the map dependency property within my usercontrols. I am able to pass values and populate layers, tables, timeslider data. However,  I continue to run into issues when I have controls(Sliders) which are located in a usercontrol and are suppose to effect a graphic layer in the Xaml mainpage. 

I'm using a userControl defined as QueryPanel. I am able to run queries within the User Control, and display the graphic results in the MainPage. In the UserControl I am also providing a TimeSlider and seperate Slider for a Heatmap function. However, I cant seem to get either slider to manipulate the layer in the MainPage. I have tested the code, by placing all of the related code within the Mainpage.xaml and cs and it works fine. Just not sure if I am missing something, when I attempt to transfer the sliders into a Usercontrol

Using the below Map Dependency in UserControl   QueryPanel.CS

protected static readonly DependencyProperty MapProperty=
            DependencyProperty.Register("Map", typeof(Map), typeof(QueryPanel), new PropertyMetadata(OnMapPropertyChanged));

        private static void OnMapPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ( protected static readonly DependencyProperty MapProperty=
            DependencyProperty.Register("Map", typeof(Map), typeof(QueryPanel), new PropertyMetadata(OnMapPropertyChanged));

        public Map Map
        {
            get { return (Map)this.GetValue(MapProperty); }
            set
            {
                this.SetValue(MapProperty, (DependencyObject)value);               
            }
        }

        private static void OnMapPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            QueryPanel control = d as QueryPanel;
Map newMap = e.NewValue as Map;
if (control != null)
{
control._selectionGraphicslayer = newMap.Layers["Incidents"] as GraphicLayer; //Graphic layer being populated from query
TimeSlider IncidentTimeSlider = control.IncidentTimeSlider as TimeSlider; //Not sure if this is correct, wasnt sure how to connect TimeSliders
control.IncidentTimeSlider = IncidentTimeSlider as TimeSlider;

}

In UserControl.xaml
<esri:TimeSlider  x:Name="IncidentTimeSlider"
                       TimeMode="TimeExtent"
                       //wasnt sure if I was supposed to provide map binding here
                      Value="{Binding ElementName=Map}" //If so, should I also include a Map Control within the Query Panel UserControl???

In MainPage assigning Incident Time slider to Map
<ESRI:Map TimeExtent="{Binding ElementName=IncidentTimeSlider, Path=Value}"
additional map code
</ESRI:Map>
      

Had to manually add some of this code, so let me know if you have any questions.
0 Kudos