+ Reply to Thread
Results 1 to 5 of 5

Thread: Default to collapsed legend items

  1. #1
    Cory Schinkel
    Join Date
    Jul 2010
    Posts
    10
    Points
    6
    Answers Provided
    1


    0

    Default Default to collapsed legend items

    Hi,

    Is there a property I can set so that items in the legend default to collapsed? I am using the legend in tree mode and consuming a map service that has 30 layers in it. When the layer loads the tree node expands and I can't find a way to prevent this (short of using control templates).

    Thanks,
    Cory

  2. #2
    Mike Branscomb

    Join Date
    Feb 2010
    Posts
    880
    Points
    359
    Answers Provided
    59


    0

    Default Re: Default to collapsed legend items

    Hi,

    One approach is to follow this example in the SilverLight forum: http://forums.arcgis.com/threads/439...end-by-default

    Cheers

    Mike
    ArcGIS Runtime Product Engineer

  3. #3
    Cory Schinkel
    Join Date
    Jul 2010
    Posts
    10
    Points
    6
    Answers Provided
    1


    0

    Default Re: Default to collapsed legend items

    Mike,

    Thanks for your response. I considered handling the legend refreshed event, however I am using MVVM and that approach does not work well with the pattern. I may have a way to get around the MVVM issue, but it's not ideal. Do you have any suggestions on how to handle the event while maintaining strict adherence to MVVM (no code-behind)?

    Thanks,
    Cory

  4. #4
    Bjorn Kuiper
    Join Date
    Oct 2010
    Posts
    270
    Points
    25
    Answers Provided
    6


    0

    Default Re: Default to collapsed legend items

    Quote Originally Posted by schinks View Post
    Mike,

    Thanks for your response. I considered handling the legend refreshed event, however I am using MVVM and that approach does not work well with the pattern. I may have a way to get around the MVVM issue, but it's not ideal. Do you have any suggestions on how to handle the event while maintaining strict adherence to MVVM (no code-behind)?

    Thanks,
    Cory
    I guess you would be able to solve this by implementing a WPF behavior using attached properties.

  5. #5
    Cory Schinkel
    Join Date
    Jul 2010
    Posts
    10
    Points
    6
    Answers Provided
    1


    1
    This post is marked as the answer

    Default Re: Default to collapsed legend items

    A colleague and I came up with a solution to this problem. I wanted to use EventTriggers to handle the Legend Refresh event in my ViewModel, the problem is that EventTriggers do not support passing EventArgs. That is another topic all together, why event args are not passed with the EventTrigger. Anyway, my colleague extended the EventTrigger so that it supports passing the event args and it works great.

    I will attempt to explain how it all comes together. In the view, a trigger is added to the legend control:
    Code:
    <esri:Legend Map="{Binding ElementName=MyMap}"
    LayerItemsMode="Tree"> <i:Interaction.Triggers>
    <i:EventTrigger EventName="Refreshed" >
    <Commands:InvokeCommandActionWithArgs Command="{Binding LegendRefreshedCommand}" />
    </i:EventTrigger>
    </i:Interaction.Triggers>
    </esri:Legend>


    The key thing to notice here is Commands:InvokeCommandActionWithArgs. This is an extended version of TriggerAction and looks like:
    Code:
    public class InvokeCommandActionWithArgs : TriggerAction<DependencyObject>    
    {
            public static DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand),
                                        typeof(InvokeCommandActionWithArgs));
             public ICommand Command
            {
                get { return GetValue(CommandProperty) as ICommand; }
                set { SetValue(CommandProperty, value); }
            }
             
            protected override void Invoke(object parameter)
            {
                if (Command == null) return;
                Command.Execute(parameter);
            }
        }
    Also notice in the view that the command binding for the Triggeraction is Command="{Binding LegendRefreshedCommand}" . LegendRefreshedCommand is defined in our ViewModel which looks something like:

    Code:
    public class MainWindowViewModel    {
             public ICommand LegendRefreshedCommand { get; private set; }
             public MainWindowViewModel()
            {
                LegendRefreshedCommand = new RelayCommand<Legend.RefreshedEventArgs>(OnLegendRefresh, null);
            }
             
           private void OnLegendRefresh(Legend.RefreshedEventArgs refreshedEventArgs)
            {
                refreshedEventArgs.LayerItem.IsExpanded = false;
            }
        }
    


    When the Refreshed event on the legend is bound to the LegendRefreshedCommand on the viewmodel via the extended TriggerAction the OnLegendRefresh method will handle the event. The RefreshedEventArgs are passed in and the LayerItem can now be accessed and the IsExpanded property set to false. This is the piece that is missing when using the plain EventTrigger, because it does not pass the event args.

    Hopefully that all makes sense and is easy to follow. I have attached a very simple reference project that might make it more clear.

    Attached Files

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts