0

I have a TreeView whose ItemsSource is binded to StaticResource, which is a class. This has worked fine for me, but now I have a function that updates the lowest level of the TreeView with possible different values and this update needs to be shown immediately. I have accomplished similar tasks like binding IsChecked of checkboxes in the tree to a value in a model, as well as textblock text binding to values in a model.

Here is the code for reference of the TreeView in XAML:

<!-- TREE VIEW ON LEFT HAND SIDE. LOADS TREEVIEWPARENTS, WHICH HAVE ORDER ATTRIBUTE CHILDREN -->
<DockPanel Name="test1" Margin="10,10,0,10" VerticalAlignment="Stretch" Grid.Row="3" Grid.RowSpan="6" Grid.Column="0">
    <DockPanel.Resources>
        <local:CheckBoxCommand x:Key="cbc"></local:CheckBoxCommand>
        <src:TreeViewFilter x:Key="MyList" />

        <!-- PARENTS OF THE TREEVIEW -->
        <HierarchicalDataTemplate DataType="{x:Type src:TreeViewParent}" ItemsSource="{Binding Path=OrderAttributes}">
            <TextBlock Text="{Binding Path=NameAndCount}" FontSize="24"/>
        </HierarchicalDataTemplate>

        <!-- CHILDREN OF THE PARENTS. THESE ORDER ATTRIBUTES HAVE CHILDREN OF THEIR OWN -->
        <HierarchicalDataTemplate DataType="{x:Type src:OrderAttribute}" ItemsSource="{Binding Path=OrderAttributes}">
            <StackPanel Name="test" Orientation="Horizontal" VerticalAlignment="Center">
                <CheckBox Command="{StaticResource cbc}"
                            CommandParameter="{Binding Path=NameAndParent}" Visibility="{Binding Path=CheckBoxVisible}" IsChecked="{Binding Path=isChecked}" VerticalAlignment="Center">
                </CheckBox>
                <TextBlock Text="{Binding Path=NameAndCount}" FontSize="16"/>
             </StackPanel>
        </HierarchicalDataTemplate>
    </DockPanel.Resources>

    <TreeView Name="treeView1" BorderThickness="2" ItemsSource="{Binding Source={StaticResource MyList}, NotifyOnSourceUpdated=True}" TreeViewItem.Selected="filterByBatchStatus"/>
</DockPanel>

So as you can see, the ItemsSource is binded to a StaticResource MyList, which is really just a key for a Name of the class TreeViewFilter. The reason this has been working for me is because The "TreeViewParents" and the "OrderAttributes" that the tree contains are all created in the constructor of the TreeViewFilter class. But now I want to be able to update values in the lowest heirarchy of the tree and have them be shown visibly.

My guess is that I can do this similar to how I did other bindings with visual updates, using INotifyPropertyChanged and firing propertyChanged events or something along those lines? Any Ideas?

(Also, that NotifyOnSourceUpdated=True in the binding is something I was just messing with for this problem, dont know how that works)

Kevin Quiring
  • 497
  • 9
  • 22

1 Answers1

0

Is myList an ObservableCollection? This class has built-in notifications.

Implmentation example taken from this poorly formatted MSDN article: http://msdn.microsoft.com/en-us/library/ms748365.aspx

public class NameList : ObservableCollection<PersonName>
{
    public NameList() : base()
    {
        Add(new PersonName("Willa", "Cather"));
        Add(new PersonName("Isak", "Dinesen"));
        Add(new PersonName("Victor", "Hugo"));
        Add(new PersonName("Jules", "Verne"));
    }
  }

  public class PersonName
  {
      private string firstName;
      private string lastName;

      public PersonName(string first, string last)
      {
          this.firstName = first;
          this.lastName = last;
      }

      public string FirstName
      {
          get { return firstName; }
          set { firstName = value; }
      }

      public string LastName
      {
          get { return lastName; }
          set { lastName = value; }
      }
  }

And here's a couple links to help you with the concept of using TreeView with an observableCollection: http://msdn.microsoft.com/en-us/library/dd759035(v=vs.95).aspx

Mike Hillberg's Blog

Bill Tarbell
  • 4,128
  • 2
  • 29
  • 48
  • Yes it is an observable collection like you have demonstrated. I am more confused with how I write the proper XAML code to update the UI when the ItemsSource changes, since the ItemsSource currently is bound to the TreeViewFilter class (and the objects are created originally in the constructor) – Kevin Quiring Nov 12 '12 at 20:10
  • I'm confused. Are you pointing TreeView ItemsSource to a new instance or changing the ItemsSource the TreeView references (and how)? – Rich Nov 12 '12 at 20:13
  • Have you seen the following link? It seems like it may be applicable, but i cannot tell without seeing your list's code. http://stackoverflow.com/questions/2138281/wpf-treeview-bound-to-observablecollection-not-updating-root-nodes – Bill Tarbell Nov 12 '12 at 20:15
  • I am pointing TreeView ItemsSoure to an instance (the only instance) of TreeViewFilter class, which creates the TreeView objects in the constructor. I now have a function in this TreeViewFilter class that updates the objects that are orginally created in the constructor and I need this new update to be updated in the UI of the tree. – Kevin Quiring Nov 12 '12 at 20:20