Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1585
Binding XamTreeItem IsExpanded
posted

I am trying to in the IsExpanded property to be bound two way so that the tree stays persistent throughout the life of the application.

 

<igTree:XamTreeItem MouseLeftButtonDown="XamTreeItem_MouseLeftButtonDown"
                    Visibility="{Binding Path=Visible, Mode=TwoWay}" 
                    IsExpanded="{Binding Path=IsExpanded, Mode=TwoWay}"
                    IsSelected="{Binding Path=IsSelected, Mode=TwoWay}"
                    PropertyChanged="XamTreeItem_PropertyChanged"/>

Initially I have the value set as TRUE.
But after binding happens somehow all the values are false...

I am never setting IsExpanded anywhere in code except the constructor
which initializes it to TRUE.

The only time the value can be changed is on the PropertyChanged event for the XamTreeItem

This code is executed for this...

private void
XamTreeItem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
XamTreeItem
 xamTreeItem = (XamTreeItem)sender;
 if (e.PropertyName == "IsExpanded")
 {
  ((TreeViewItemInstance)xamTreeItem.Data).IsExpanded =
xamTreeItem.IsExpanded;
 }
}

So I am not sure how things are getting set to false. When I initially
create my collection that will be bound as the model to bind to each
item's IsExpanded is set to TRUE. Then after the initial binding
somehow they all get set to FALSE. Any ideas?
I also thought I could use the persistence class to maintain a property of a
control throughout its life... I tried this...


<igTree:XamTreeItem MouseLeftButtonDown="XamTreeItem_MouseLeftButtonDown"
Visibility="{Binding Path=Visible, Mode=TwoWay}"                                
IsSelected="{Binding Path=IsSelected, Mode=TwoWay}"                    
   PropertyChanged="XamTreeItem_PropertyChanged">
  <ig:PersistenceManager.Settings>
  <ig:PersistenceSettings  SavePersistenceOptions="OnlySpecified">
   <ig:PersistenceSettings.PropertySettings>
    <ig:PropertyNamePersistenceInfo PropertyName="IsExpanded"
Options="PropertyPath"/>
    </ig:PersistenceSettings.PropertySettings>
   </ig:PersistenceSettings>
  </ig:PersistenceManager.Settings>
</igTree:XamTreeItem>

 This did not work either...

 

  • 21382
    posted

    I tried to reproduce your results with the following:

    <ig:XamTree x:Name="tree" DisplayMemberPath="Name">            

                <ig:XamTree.DefaultItemsContainer>

                    <DataTemplate>

                    <ig:XamTreeItem 

                        Visibility="{Binding Path=Visible, Mode=TwoWay}" 

                        IsExpanded="{Binding Path=IsExpanded, Mode=TwoWay}"                    

                        DisplayMemberPath="Name"

                        ItemsSource="{Binding Path=Stuff}"

                        />

                    </DataTemplate>

                </ig:XamTree.DefaultItemsContainer>

            </ig:XamTree>

     

    With a data class defined

     

     public class DataObject : INotifyPropertyChanged

        {

            public Visibility Visible { get; set; }

     

            private bool _isExpanded;

            public bool IsExpanded 

            {

                get { return this._isExpanded; }

     

                set

                {

                    if (this._isExpanded != value)

                    {

                        this._isExpanded = value;

     

                        if (this.PropertyChanged != null)

                        {

                            this.PropertyChanged(this, new PropertyChangedEventArgs("IsExpanded"));

                        }

                    }

                }

            }

            public string Name { get; set; }

     

            public ObservableCollection<DataObject> Stuff { get; set; }

     

     

     

            #region INotifyPropertyChanged Members

     

            public event PropertyChangedEventHandler PropertyChanged;

     

            #endregion

        }

     

    And the list built like 

     

            public MainPage()

            {

                InitializeComponent();

     

                List<DataObject> list = new List<DataObject>();

     

                DataObject d = new DataObject();

                d.Name = "object 1";

                d.Visible = Visibility.Visible ;

                d.IsExpanded = true;

     

                d.Stuff = new ObservableCollection<DataObject>();

                d.Stuff.Add(new DataObject() { Name = "object 2" });

                list.Add(d);

                tree.ItemsSource = list;

            }

     

     

    And did not see the same behavior.  The expanded node was expanded when the page rendered and i validated that changing is expanded via the ui changed the underlying data object.

     

    Are you adding your data piecemeal or all prior to binding?   If you add the top level node prior to there being any children the tree may not know it could be expanded?  Its only a guess on to what might be happening.