Select to view content in your preferred language

What's wrong with my code?

687
4
Jump to solution
08-20-2012 07:37 AM
DavidAshton
Frequent Contributor
Sorry for posting this problem a second time but no one replied to my first post and it was just me talking to myself.  The post was running long so I thought I would start something new and hope someone might help out.

I working with the TabbedRibbon Template.  I commented out the code for the DataGrid x:Name="QueryDetailsDataGrid"
and added code to use a FeatureDataGrid = MyFDG instead. I added a SelectionChanged="MyDataGrid_SelectionChanged" Event and I'm running into an  issue I need help with.

I know the selection change event is firing correctly so it is not a problem with that event but a problem with my code. I'm attaching the zip file with the xaml and C# Main Pages in hopes someone can look at. You can down load the tabbed ribbon template and swap out my xaml and C# page with the originals and should be rockin, but in short here's the deal:

Below is my code in question and on the First click on a row in FDG the SelectionChanged code is executed but the code inside the for loop does not execute but on the second click/ selection change of the row in the FDG the SelectionChanged code is executed again and the for loop finally it gets executed but the message box displays the previous state name (from the first click/selection)

 private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)                 {                                                             if (MyDataGrid.SelectedItem != null)                     {                          if (MyDataGrid.SelectedGraphics.Count == 1)                         {                             var graphics = (sender as FeatureDataGrid).SelectedGraphics;                             foreach (var g in graphics)                             {                                 // get id                                  var id = g.Attributes["STATE_NAME"];                                  StateName = (string)id;                                                                  break;                             }                             MessageBox.Show(StateName);                          }                     }                 } 


Here's a link to my old post just in-case you want to review that for more detail.

Thanks Dave
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
if (e.PropertyName == "MySelectionGraphicsLayer" && MyDataGrid.SelectedGraphics.Count == 1)
MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString());


You don't have to change the name of the property. It's predefined : "SelectedGraphics"

So:
private void GraphicsLayer_PropertyChanged(object sender, PropertyChangedEventArgs e) {     if (e.PropertyName == "SelectedGraphics" && MyDataGrid.SelectedGraphics.Count == 1)         MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString()); }


and don't forget to wire up the GraphicsLayer_PropertyChanged handler in XAML.

View solution in original post

0 Kudos
4 Replies
DominiqueBroux
Esri Frequent Contributor
Maybe depending on the order the SelectionChanged events are fired.
When the datagrid selection changes, the graphics layer may have not changed yet (likely the toolkit subscribes to this event as well in order to change the graphics selection).

It might be more accurate to use the PropertyChanged event of the GraphcisLayer.
Something like:

private void GraphicsLayer_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "SelectedGraphics" && MyDataGrid.SelectedGraphics.Count == 1)
        MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString());
}
0 Kudos
DavidAshton
Frequent Contributor
Dominique, Thanks for the reply!

Maybe depending on the order the SelectionChanged events are fired.
When the datagrid selection changes, the graphics layer may have not changed yet (likely the toolkit subscribes to this event as well in order to change the graphics selection).

It might be more accurate to use the PropertyChanged event of the GraphcisLayer.


O.K. that makes sense.  Not sure if you read my previous post but this exact code worked with the 2.1 API but I can't get it to work with 2.4 or 3.0. 

So I tried you suggestion but I couldn't get the if statement to be true so the next line never fires.  I had to change it slightly to match the graphics layer that I'm binding to my FDG.

 if (e.PropertyName == "MySelectionGraphicsLayer" && MyDataGrid.SelectedGraphics.Count == 1)
            MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString());


Sorry I couldn't get it...is there anything else you can show/tell me?

Thanks
David
0 Kudos
DominiqueBroux
Esri Frequent Contributor
if (e.PropertyName == "MySelectionGraphicsLayer" && MyDataGrid.SelectedGraphics.Count == 1)
MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString());


You don't have to change the name of the property. It's predefined : "SelectedGraphics"

So:
private void GraphicsLayer_PropertyChanged(object sender, PropertyChangedEventArgs e) {     if (e.PropertyName == "SelectedGraphics" && MyDataGrid.SelectedGraphics.Count == 1)         MessageBox.Show(MyDataGrid.SelectedGraphics.First().Attributes["NAME"].ToString()); }


and don't forget to wire up the GraphicsLayer_PropertyChanged handler in XAML.
0 Kudos
DavidAshton
Frequent Contributor
Thanks Dominique!
0 Kudos