Reorder layers in the Legend control

3429
7
Jump to solution
04-05-2013 01:40 PM
Labels (1)
GeorgeFaraj
Occasional Contributor III
What's the best way to allow the user to re-order the layers in the map by dragging and dropping the layer name in the Legend control? This was possible in ArcObjects and it would be very useful now.

Any ideas?
0 Kudos
1 Solution

Accepted Solutions
DominiqueBroux
Esri Frequent Contributor
The legend Template sets these style bindings for the default tree view style:
<TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/> </Style> </TreeView.ItemContainerStyle>

So the treeview expanded state is binded to the LayerItemViewModel.IsExpanded property.
As the default value for LayerItemViewModel.IsExpanded is true, the tree nodes get expanded by default.

If you want to change that, you can either remove the binding in the style (but you won't be able to change the 'expanded' state of an item by code), or you can set LayerItemViewModel.IsExpanded to false by code (likely on event Legend.Refreshed)

View solution in original post

0 Kudos
7 Replies
DominiqueBroux
Esri Frequent Contributor
A few time ago, I did such a tentative in this KML sample.
Might help as a starting point but would likely need to be enhanced.
0 Kudos
GeorgeFaraj
Occasional Contributor III
That's exactly what I need to do. Thanks a lot, I will be trying it out soon.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Hey Dominique,

You're using the following references that appear to be only for Silverlight:

System.Windows.Controls.Toolkit
System.Windows.Controls.Input.Toolkit

Are these assemblies available for Windows apps, or what alternative do you suggest?

Also, I can't seem to find TreeViewExtended in the ESRI primitives assembly. I'm using 10.1.1.

Thanks,
George
0 Kudos
DominiqueBroux
Esri Frequent Contributor
It seems the DragDrogTargetTreeView Silverlight control that I am using in this sample doesn't exist in the WPF toolkit.

It's possible to manage drag and drop in WPF as well but it's likely more difficult than what I did in SL.
Sorry for sending you in the wrong direction.

Note: Concerning TreeViewExtended, it was built to workaround some limitations in SL and is not needed in WPF. So we can use the standard TreeView instead.
0 Kudos
GeorgeFaraj
Occasional Contributor III
Hi Dominique,

I ended up not doing the drag&drop since my end result was not good enough and didn't look quite nice enough. It would take too long to get right and we don't have much time. But I still kept some of the control customization that you had in your sample app. I'm using a TreeView instead of a TreeViewExtended, per your suggestion.

I had a question, though. How did you make it so that tree nodes get expanded automatically when the layer is added?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The legend Template sets these style bindings for the default tree view style:
<TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/> </Style> </TreeView.ItemContainerStyle>

So the treeview expanded state is binded to the LayerItemViewModel.IsExpanded property.
As the default value for LayerItemViewModel.IsExpanded is true, the tree nodes get expanded by default.

If you want to change that, you can either remove the binding in the style (but you won't be able to change the 'expanded' state of an item by code), or you can set LayerItemViewModel.IsExpanded to false by code (likely on event Legend.Refreshed)
0 Kudos
GeorgeFaraj
Occasional Contributor III
That's it, I had to add that style setters since I'm specifying my own ControlTemplate. Thanks a lot!
0 Kudos