Select to view content in your preferred language

MeasureAction Binding Units

2289
7
08-27-2010 06:17 AM
WilliamKimrey
Emerging Contributor
Hello all,

I noticed something today that seems like an oversight, but I wanted to see if anybody else has run into this.

In SL4, I have a MeasureAction with the DistanceUnit bound to a ComboBox that contains several different measurement units.  This works great for the most part.  The problem I'm having is that I'm trying to do the same thing for the AreaUnit.  There's no Binding option for this field even though there's a Binding option for every other field in the MeasureAction.

Is there something particularly wrong with the AreaUnit or a reason why it did not recieve Binding support?

Thanks,
Will
0 Kudos
7 Replies
JenniferNery
Esri Regular Contributor
AreaUnit in MeasureAction is also a DependencyProperty, which allows it to be bindable just as DistanceUnit. However, you cannot bind them to the same combobox because they take different enum values. Could this be the issue? If not, what error message do you get and can you share us your code?  Thanks.
0 Kudos
WilliamKimrey
Emerging Contributor
Thanks for the reply.  I'm not Binding them to the same ComboBox, I've got one for DistanceUnit and one for AreaUnit. 

The error I'm getting is "XamlParseException" with the Line and Position of the error placed right before the Binding on the AreaUnit.  The InnerException's Message says "Object of type 'SYstem.Windows.Data.Binding; cannot be converted to type 'ESRI.ArcGIS.Client.Actions.AreaUnit'.

Here's the relevant code:

<Button x:Name="AreaBtn" Content="Polygon">
     <i:Interaction.Triggers>
          <i:EventTrigger EventName="Click">
                <esriActions:MeasureAction TargetName="MyMap"
                                                      MapUnits="Feet"
                                                      MeasureMode="Polygon"
                                                      DistanceUnit="{Binding ElementName=UnitCombo, Path=SelectedItem.Content}"
                                                      AreaUnit="{Binding ElementName=AreaCombo, Path=SelectedItem.Content}"/>
          </i:EventTrigger>
      </i:Interaction.Triggers>
</Button>

<ComboBox x:Name="UnitCombo">
       <ComboBox.Items>
              <ComboBoxItem Content="Feet" IsSelected="True"/>
              <ComboBoxItem Content="Meters"/>
              <ComboBoxItem Content="Miles"/>
       </ComboBox.Items>
</ComboBox>

<ComboBox x:Name="AreaCombo">
       <ComboBox.Items>
              <ComboBoxItem Content="SquareFeet" IsSelected="True"/>
              <ComboBoxItem Content="SquareMeters"/>
              <ComboBoxItem Content="SquareMiles"/>
       </ComboBox.Items>
</ComboBox>
0 Kudos
dotMorten_esri
Esri Notable Contributor
The content you are binding to is a TextBlock (your string will automatically be converted to a textblock), not an AreaUnit.

Try creating an array of the the enums (not strings!) and bind to the combobox's ItemsSource.
0 Kudos
WilliamKimrey
Emerging Contributor
OK, I'll give that a shot, but why does this same method work with the DistanceUnit?  What's different about the DistanceUnit and AreaUnit that binding works for one but not the other.
0 Kudos
JenniferNery
Esri Regular Contributor
Thank you for reporting this. AreaUnit binding was failing because the registered DependencyProperty name did not match the property name. We'll be sure to fix this. Thanks again.

I tried similar code:
XAML
        <StackPanel VerticalAlignment="Top" HorizontalAlignment="Center" Orientation="Horizontal">
            <ComboBox x:Name="DistanceUnitCB" />
            <ComboBox x:Name="AreaUnitCB" />
            <Button Content="Measure">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <esri:MeasureAction AreaUnit="{Binding ElementName=AreaUnitCB, Path=SelectedItem}" DisplayTotals="True" DistanceUnit="{Binding ElementName=DistanceUnitCB, Path=SelectedItem}" MapUnits="DecimalDegrees" MeasureMode="Polygon" FillSymbol="{StaticResource DefaultFillSymbol}" TargetName="MyMap" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </StackPanel>


Code-behind:
  public MainPage()
        {
            List<DistanceUnit> distanceUnits = new List<DistanceUnit>();
            distanceUnits.Add(DistanceUnit.DecimalDegrees);
            distanceUnits.Add(DistanceUnit.Feet);
            distanceUnits.Add(DistanceUnit.Kilometers);
            distanceUnits.Add(DistanceUnit.Meters);
            distanceUnits.Add(DistanceUnit.Miles);
            distanceUnits.Add(DistanceUnit.NauticalMiles);
            distanceUnits.Add(DistanceUnit.Undefined);
            distanceUnits.Add(DistanceUnit.Yards);
            List<AreaUnit> areaUnits = new List<AreaUnit>();
            areaUnits.Add(AreaUnit.Acres);
            areaUnits.Add(AreaUnit.Hectares);
            areaUnits.Add(AreaUnit.SquareFeet);
            areaUnits.Add(AreaUnit.SquareKilometers);
            areaUnits.Add(AreaUnit.SquareMeters);
            areaUnits.Add(AreaUnit.SquareMiles);
            areaUnits.Add(AreaUnit.Undefined);

            InitializeComponent();

            this.DistanceUnitCB.ItemsSource = distanceUnits;
            this.DistanceUnitCB.SelectedIndex = 0;
            this.AreaUnitCB.ItemsSource = areaUnits;
            this.AreaUnitCB.SelectedIndex = 0;
        }
0 Kudos
WilliamKimrey
Emerging Contributor
Ok, glad that I was able to help out inadvertantly.  Any idea of when this issue will be fixed?  Is it a ArcServer issue that needs a patch or is it a Silverlight API issue that'll be fixed in the next release?

I guess for now I'll see if I can set the property on the ComboBox SelectedItem_Changed event.
0 Kudos
JenniferNery
Esri Regular Contributor
This will be fixed in the next release of our API for SL/WPF.
0 Kudos